Preface:Uphold as a newcomer, to have "where to fight where" consciousness. In the boss's demand, the top front of the vacancy first dry array front end. Of course, in the rest of the work or to a well-established direction to work hard, after the end of the internship computer, the graduation to change the new Mac, re-take the environment of go, by the way to summarize the next, with the depth of study, will continue to update.
Go download:Go language download: Https://studygolang.com/dl mac directly through the brew instatll go instructions to complete the download installation
GO environment variable configuration:$GOROOT =/usr/local/go Go installation directory $GOPATH =/usr/local/var/www/go go working directory $GOBIN = $GOPATH/bin Go executable Directory $PATH = $PATH: $GOBIN: $GOROOT/bin Add the go executable to PATH so that the go command and the Go app we write can be called globally
$GOPATH The working directory structure, there are three subdirectories (which need to be created yourself):src--storing the source code file pkg--storing the compiled file bin--storing the compiled executable file
Example (first build your app with Go-recommended go build, and later you'll talk about other ways):With our operations, after the final Test succeeds, the $GOPATH directory tree should look like this:
Here's how:1. Create an App package mypkg:Cut to the "$GOPATH/src" directory, create a mypkg directory, in the Mypkg directory, a new hello.go, the content is as follows:
2, compile the application package (in this demo, this step is not necessary, just to show the Go Install compilation effect):Execute the "Go Install" command in just the directory or execute the "Go install mypkg" command in any directory, if the code is wrong or depends on the error, if correct, there is no prompt. At this point, you can cut into the "$GOPATH/pkg" directory, you will find a directory called "$GOOS _$goarch", cut into the directory, you will see just compiled the generated file Mypkg.a file. (where the specific values of $goos and $goarch can be queried through the "Go env" command, for more details see: http://wiki.jikexueyuan.com/project/go-command-tutorial/0.14.html)
3, create a new application package MyApp, create a new file Main.go (same step 1), Import introduced MYPKG package, the contents are as follows:
4. Build the executable file:In the MyApp directory, the "Go Build" command will generate a MyApp executable file in the current directory, which does not depend on other files and can be copied to any directory through "./myapp" and can be copied to any $path configuration directory. MyApp "Global execution.
Summary:In this demo, we used two go instructions, "Go Install" and "Go Build", their differences are as follows:
The function of the Go Build command is:Compiles the executable file in the current directory. Note that the go Build command will call all the source code of the reference package, recompile, instead of directly using the PKG in the post-compilation file, if the "$GOROOT" and "$GOPATH" did not find the import into the package project source code, will be error. In this demo, Main.go import introduced the "MYPKG" package, if I delete "$GOPATH/src/mypkg" source code, execute "Go Build" command, will be error as follows:
The function of the "Go Install" command is:Compile the source code, if the executable file (the package "main" and contains the Main method), the build executable file will be compiled into the "$GOPATH/bin" directory, the executable file import introduced by the other packages, will be compiled to "$GOPATH/pkg/$GOOS _$goarch "directory. (On the Go import and compile, if there is unclear, you can look at this blog https://studygolang.com/articles/3189)
we will find:Go install can also do something like "Go build", except that "go install" will generate binary files to "$GOPATH/pkg/$GOOS _$goarch" directory (this binary file doesn't make sense to us), but "go Install "will automatically generate the executable file into the" $GOBIN "directory, which was initially configured in the" $PATH ", so that the project executable can be convenient for global invocation.
If you are using go install to build an executable file in this demo: When we write the source code file, the directory tree is as follows:
Cut to the MyApp directory and execute the "Go Install" command, which will generate the following structure directory tree:
This can be done globally through "MyApp".
Other directives:Go env command to check GO environment
Go version View Go versions
Go get can be temporarily understood as Git clone to "$GOPATH/src" + Go Install
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.