4

Hi I'm pretty new at Golang, after install it I would like to use the next package in my project: https://github.com/gin-gonic/gin

After I created my project, I did the next command to install gingonic:

go get -u github.com/gin-gonic/gin

But the import is not recognized inside my project, I understand that it's something related with my GOROOT, but I wasn't able to solve the issue.

The next are are my Go env variables:

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/rpantoja/Library/Caches/go-build"
GOENV="/Users/rpantoja/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/rpantoja/go/pkg/mod"
GONOPROXY="github.com/mercadolibre"
GONOSUMDB="github.com/mercadolibre"
GOOS="darwin"
GOPATH="/Users/rpantoja/go"
GOPRIVATE="github.com/mercadolibre"
GOPROXY="http://goregistry.furycloud.io/"
GOROOT="/usr/local/Cellar/go/1.15/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gz/zfy97n595rs5w_t0dr9wr29dzzxvs4/T/go-build960054223=/tmp/go-build -gno-record-gcc-switches -fno-common"

And this is how my project it's configured:

enter image description here

enter image description here

enter image description here

º

After install the package:

enter image description here

rasilvap
  • 1,771
  • 3
  • 31
  • 70
  • 1
    What error does the compiler report? There's a history here on S.O. of unexplained problems with the Homebrew Go installation. Consider switching to [the official Go installation](https://golang.org/dl/). – Charlie Tumahai Dec 03 '20 at 22:28
  • Did you ```go init``` the module file in the root of the project? – Margach Chris Dec 03 '20 at 23:37
  • Don't use Homebrew's Go. Properly set up your project according to "How to Write Go Code". Build your project on the command line. Don't post images. – Volker Dec 04 '20 at 06:38
  • If you are using GOPATH instead of Go Modules, please, disable Go Modules integration in the settings. You can also hover over import and execute `Option+Enter` shortcut, then choose `go get ...` – s0xzwasd Dec 04 '20 at 11:04
  • Another way with Go Modules: execute `go mod init` in built-in Terminal, then disable GOPATH indexing (`Preferences | Go | GOPATH | Index entire GOPATH`) and hover over import, `Option+Enter`, Sync Dependencies... – s0xzwasd Dec 04 '20 at 11:06

2 Answers2

5

The steps to do to set up a new Go project with modules:

  1. Have Go installed. Latest version preferably, best >= v1.13 which Go modules the default. For go1.11 and above you will have to do some extra steps to enable Go modules.
  2. Create a new folder for your project. Preferably NOT in GOPATH. By default GOPATH is ~/go, so create your own projects folder, e.g. mkdir ~/projects and then mkdir ~/projects/myproject.
  3. All further commands are run from the new projects root, so best switch there: cd ~/projects/myproject
  4. In the newly created folder run go mod init projectPath where projectPath should be the URL of your future git repo (e.g. github.com/myname/myproject). This will create the go.mod file in the current folder. It will contain the module name you used in go mod init and the currently installed go version as a minimum version. (Don't worry about that for now, it won't get in your way.) If you don't plan on ever releasing your project, you can name your project anything. But if that ever conflicts with another package or module name, you are in trouble.
  5. Now you can run go get github.com/gin-gonic/gin (don't use -u, that is dangerous as it update all sub-dependencies instead of using the dependencies the gin developers used). This should add github.com/gin-gonic/gin to your go.mod file as a requirement. If you want to update a dependency, just call go get depPath again. It will update the dependency version in your go.mod file to the latest version available. If you want to up-/downgrade to a specific version use go get depPath@vX.Y.Z.
  6. Create your main.go and use github.com/gin-gonic/gin in there.
  7. Use go mod tidy to remove all unused imports or add missing ones to go.mod. (Usually you don't need to edit go.mod, go mod tidy will do that for you.) It will also tidy up your go.sum file which holds check sums for all your dependencies. You can have a look at the file, but will (usually) never have to edit it. go mod tidy will do that for you.
  8. In Goland the most important is to make sure Go modules integration is enabled. The other settings should be correct by default.
  9. If you still have problems with the dependencies, you can try a go clean -modcache. It will clear your entire local modules cache, so you need to download all of it again. This can sometimes help if the modules cache got messed up somehow. Should not happen normally, though.

Hope this helps. If it doesn't, let me know so I can add the missing parts.

TehSphinX
  • 6,536
  • 1
  • 24
  • 34
  • I am a bit surprised by `don't use -u, that is dangerous as it update all sub-dependencies instead of using the dependencies the gin developers used`. I checked in `go help get`, it is not very clear (to me). Do you have references ? –  Dec 04 '20 at 14:11
  • On the `go get` documentation: https://golang.org/pkg/cmd/go/internal/get/ it says `-u` will update your package and all dependencies. If your dependency has a `go.mod` file stating which versions of the dependencies it needs, `-u` will ignore those and update the sub-dependencies anyway. That's why I said it is "dangerous". There should in theory never be a problem as all higher versions of the same major version should be compatible, but... you know: reality is not that simple. – TehSphinX Dec 04 '20 at 14:18
  • to clarify, this is about constraint with floating compatibility in their version constraints (IE: 1.X.X). So this is the exepcted behavior. Though i agree this requires trust in the library authors, which we can resume as "dangerous". –  Dec 04 '20 at 16:35
  • I'm not talking about malicious intent by the authors. I'm talking about the gin authors creating a version of gin that is well tested with version X of dependency Y and 3 weeks later a new version is released by dependency Y that changes some behaviour slightly affecting the gin package. If you use `go get -u` Go will use that new version of dependency Y even thought the `go.mod` of gin still uses the older version as that is the tested and stable release. Of course this is a theoretical case (example: gin). I just know that I ran into this with my own modules at work. – TehSphinX Dec 04 '20 at 22:13
  • I also saw today, that the `gin` page itself states to use `-u` flag. So it will probably be fine in this case. I wouldn't do it with all dependencies though. Especially some that have aged and aren't updated as frequently. – TehSphinX Dec 04 '20 at 22:15
  • 1
    yes, I agree about the scenario you are describing. nothing malicious here. This is just semver. Yes it can break IRL. –  Dec 04 '20 at 22:19
1

You should just use

go mod init 'yourmodulename' (e.g. go mod init github.com/smsa/testproject)

and then get (download) your package

go get 'yourpckagename' (e.g. go get github.com/gin-gonic/gin)