导入本地包

My project structure looks like this:

--/project
----main.go
----/models
------user.go

In main.go I want to user user.go:

user.go:

package models

type User struct {
    Name string
}

main.go:

package main

import (...)

func main() {
    user := &User{Name: "Igor"}
}

How do I import user.go from main.go?

/project is under GOPATH, so I tried:

import "project/models"

but that does nothing.

Your setup is right, you are using the package wrong.

change:

user := &User{Name: "Igor"}

to:

user := &models.User{Name: "Igor"}

or if you don't want to always say models.XXX, change your import to be.

import . "project/models"

I do find that doing this makes the code a bit harder to read, long term. It's obvious to the reader where "models.User" comes from, not so much with a plain "User" as normally that means it comes from this package.

If you are building the project outside of a go workspace you can use relative imports:

import "./models"

However, using relative imports is not good idea. The preferred way is to import the full package path (and putting your project in a proper go workspace):

import "github.com/igor/myproject/models"