Friday, 20 December 2024

GO LANG GO GET VS GO INSTALL VS GO MOD TIDY

 https://www.reddit.com/r/golang/comments/x722i0/go_install_vs_go_mod_tidy_vs_go_get/

https://stackoverflow.com/questions/24878737/what-is-the-difference-between-go-get-and-go-install


  • You use go get to add libraries to go.mod
  • those libraries can then be imported in .go file
  • those libraries are not yet being built, it can only be used when you do go mod tidy(like npm install), and you usually use go mod tidy before go build

  • when you want to use a library locally before building, you can use go install
    • Perfect example is complieDamone, you use go get, but you still wana use it to watch for changes before complie, then you do a go install




go mod tidy is the equivalent of npm install. it will fetch any dependency on your code base. it updates go.mod if necessary (there are some cases where dependency is in the code base but not go.mod)


In Go 1.18, go get will no longer build packages; it will only be used to add, update, or remove dependencies in go.mod. Specifically, go get will always act as if the -d flag were enabled.


So Basically,
you use go get to add a bunch of libaries in go.mode file

when you compile & build go binaries using go mod tidy, you install all the libraries you specified in go get, and also after using go get, you can import those libraries like in main.go in import{  "github.com/gin-gonic/gin" }
      ./src/main.go
go mod tidy
go get -u ./...
CGO_ENABLED=0 go build



No comments:

Post a Comment