This is a creation in Article, where the information may have evolved or changed.
Working directory
First of all, the Golang working directory, after the installation of Golang, one thing to do is to set up a working directory, all projects, tools, third-party packages will be stored in the working directory. By default, the working directory is the folder for the user directory go
. We can use environment variables GOPATH
to change the location of the Golang working directory.
The structure of the working directory is as follows. A bin
folder holds the various tools that the project will use, the library files required for the various packages required for the project, and pkc
src
the source code for the project. If a third-party library is used, the third-party library is also stored in the working directory.
bin/pkg/src/
Go command
Here's the GO command, which will be the main command we use. If you need to see Help, you can enter go
it directly, and the terminal will list all the commands and brief prompts. I simply translated the Help information.
PS C:\Users\asddf> goGo is a tool for managing Go source code.Usage: go command [arguments]The commands are: build 编译包和依赖 clean 移除对象文件 doc 显示宝或符号的文档信息 env 打印go环境信息 bug 提交bug信息 fix 在包上运行go tool fix fmt 在包源代码上运行格式化工具 generate 处理源代码并生成go文件 get 下载和安装包和依赖 install 编译并安装包和依赖 list 列出包 run 编译和运行go程序 test 测试包 tool 运行指定的go工具 version 打印go版本 vet 在包上运行go工具审查
Run the project
Single File
If you only need to run a single file, you can simply use the go run
command. For example, the following code file is hello_world.go
, then just need to run simply go run hello_world.go
. Here, the package of the files that can be run must be main
, and there is a main
function.
package mainimport ( "fmt")func main() { fmt.Println("Hello World")}
Complex projects
If we are writing a more complex project with a lot of packages, then we can't put the project at random. In order for a package in a project to be accessible, the project must be placed in the working directory of Go, and more specifically in the working directory src
.
For example, my project can be placed in go_workspace\src\yitian\golang-study\golang-programming
. Here, in order to differentiate between different third-party packages, a different directory hierarchy is required, such as the third-party library on most GitHub, at the directory level github.com/XXX
.
Package
The concept of the package I put to the end, because I do not know how to say. However, if you learn Java, it should be easy to understand the concept of the package, anyway, is used to classify the code. It is important to note that the Java package is used to organize the code, so the Java package name must be unique, which is done by a long list of qualified names in front of the package abc.xyz.XXX
.
Unlike Java, packages in Golang can have duplicate names, so in order to differentiate between different packages, you need to add a path at the time of the reference. Just take the project example, if go_workspace\src\yitian\golang-study\golang-programming
there is a hello
package, then in other projects to reference the package will need to use the name yitian/golang-study/golang-programming/hello
.