$GOPATH/src
directory. How to find out the current GOPATH
value if you are not sure about it? This value can be found in the list of variables output by the go env
command.$GOPATH/src
, and I recommend to think in advance about the way in which it will lie. When a project is under the control of a version control system, when it is tightened using, for example, the go get
command, it should fall precisely along the path that we defined for the project initially. For example, the service code that is stored in my github account in the rumyantseva / mif repository is deployed inside my $GOPATH/src/github.com/rumyantseva/mif
. If the same code were inside the mif repository of some private storage example.com in the namespace services, then the path to it on the developer's machine would look like $GOPATH/src/example.com/services/mif
. In order to avoid future problems or ambiguities, the code positioning rule must be observed.GOPATH
directory and within several. Accordingly, in the second case, the GOPATH
value GOPATH
have to be redefined. In order to do this, you will need to reset the corresponding environment variable to the desired value. In case GOPATH
not specified at all, Go will consider the go
directory in the user's home directory as the working directory. To better understand this, let's conduct several experiments with the console: $ # GOPATH $HOME/go: $ go env | grep GOPATH GOPATH="/Users/elena/go" $ $ # GOPATH , : $ GOPATH=/Users/tmp/something $ go env | grep GOPATH GOPATH="/Users/tmp/something" $ $ # go env: $ GOPATH=/pampam go env | grep GOPATH GOPATH="/pampam" $ $ # GOPATH - : elena:~ $ go env | grep GOPATH GOPATH="/Users/tmp/test" $ $ # GOPATH , : $ GOPATH= $ go env | grep GOPATH GOPATH="/Users/elena/go" $ # :)
GOPATH
. You can pull in dependencies automatically by calling commands such as go get
or go install
inside the current working project. At the same time, we will download the code of dependencies located in the default branches of the repositories. This is enough for the “here and now” situation, but in the general case, no one guarantees that after 5 minutes, back-incompatible changes will not appear in the same branches of external dependencies. So, the next attempt to deploy the application (for example, on the build machine) can end in failure. What will help us in this situation? Of course, vendoring.vendor
directory was repeatedly written on Habré, and in many other places, and I will not repeat. However, I’ll briefly remind you that all the dependencies that we pulled into GOPATH/src
can also be added to the vendor directory of the current application. How to do it? Or manually, or with the help of a dependency management manager. As an example of a utility for working with dependencies, we can finally mention dep , the official Go experiment. Despite the status of the "official experiment", dep is still neither absolutely stable nor recommended. Nevertheless, we ventured to try dep in our work projects, and we liked it! If this is your first time with the issue of dependency management, I highly recommend you a ten-minute video from the conference Gophercon 2017, in which the principles of the work of dep are clearly and visually shown.vendor
directory full of packages, and some set of metafiles describing our dependencies. It would seem that metafiles with a description of the used tags and even hashes of commits are enough for us, and the natural desire would be to remove the vendor
directory from the control of the version control system. However, in real production projects, you should not do this. Storing code with vendor
is the only way to protect against accidents such as dropping a github or completely removing your repository from a third-party developer.version
flag, which allows you to display information about the current version of the binary. The same practice can be useful in the case of a running service. In addition, it is sometimes useful to “sew” a binary not only the information about the semantic version, but also the hash of the commit, the build date, and other useful data.-X
flag, we can automatically replace the values of these variables.hello.go
with the following contents: package main import "fmt" var hello = "Hello" var world = "World" func main() { fmt.Printf("%s, %s!\n", hello, world) }
go run hello.go
, we get the string “Hello, World!”: $ go run hello.go Hello, World!
-X
flags and new variable values: $ go run -ldflags="-X main.hello= -X main.world=" hello.go , !
main
package, but also variables from any packages in general. Thus, during the assembly of the application, you can sew into it any necessary meta-information.-X
flagmake
utility does not lose its relevance and popularity among those who have to build applications (at least under * nix). Here and among Go-developers this tool is rather widespread.GOPATH
and make sure that it corresponds to the location of the code in the version control system. Secondly, you need to decide how to interact with the management of external dependencies and select the appropriate tool . Thirdly, it is convenient when you have at hand all the instructions that you have to perform more or less regularly and on different environments, for example, a Makefile helps in storing such instructions.Source: https://habr.com/ru/post/337158/
All Articles