In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "the method of building Go language environment". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the method of building Go language environment".
Install GE language and build Go language development environment download address
Download address of Go official website: https://golang.org/dl/
Go official image station (recommended): https://golang.google.cn/dl/
The choice of version
The executable file version is recommended for Windows and Mac platforms, and the compressed file version is recommended for Linux platform.
Install Windows installation
This installation example takes the version of Go1.11.5 executable file installed on a 64-bit Win10 system as an example.
Download the installation package selected in the previous step to your local location.
Double-click the downloaded file
Install under Linux
We select and download the go1.11.5.linux-amd64.tar.gz file on the version selection page:
Wget https://dl.google.com/go/go1.11.5.linux-amd64.tar.gz
Extract the downloaded file to the / usr/local directory:
Mkdir-p / usr/local/go # create the directory tar-C / usr/lcoal/go zxvf go1.11.5.linux-amd64.tar.gz. # decompression
If prompted with no permissions, add sudo to run again as root. After execution, you can see the go directory under / usr/local/.
Configure environment variables: there are two files under Linux that can configure environment variables, of which / etc/profile is valid for all users; $HOME/.profile is valid for the current user. Choose a file to open according to your own situation, add the following two lines of code, and save and exit.
Export GOROOT=/usr/local/go export PATH=$PATH:$GOROOT/bin
Reboot takes effect after modifying / etc/profile, and loading the $HOME/.profile file with the source command after modifying $HOME/.profile. Check:
~ go versiongo version go1.11.5 linux/amd64
Install under Mac
Download the executable version and click next to install. By default, go will be installed in the / usr/local/go directory.
Check
After the previous installation process is completed, you can open a terminal window and enter the go version command to view the installed Go version.
Configure GOPATH
GOPATH is an environment variable that indicates the storage path (working directory) of the go project you wrote.
It is best to set only one GOPATH path, and all the project code is placed in the src directory of GOPATH.
Linux and Mac platforms can simply add their own working directories to the environment variables in the same way that the environment variables are configured above. The Windows platform adds D:\ code\ go to the environment variable as follows:
Prior to Go 1.8, the GOPATH environment variable was empty by default. Starting with Go version 1.8, the Go package sets a default directory for GOPATH after installation, as shown in the following table.
Default values of GOPATH on different operating system platforms
For example, Windows%USERPROFILE%/goC:\ Users\ user name\ goUnix$HOME/go/home/ user name / go
At the same time, we add the bin directory under GOROOT and the bin directory under GOPATH to the environment variables.
Go project structure
When developing the Go language, our code is always saved in the $GOPATH/src directory. After the project has passed go build, go install or go get instructions, the downloaded third-party package source code files will be placed in the $GOPATH/src directory, the resulting binary executable files will be placed in the $GOPATH/bin directory, and the generated intermediate cache files will be saved in the $GOPATH/pkg directory.
If we use the version management tool (Version Control System,VCS. When we use Git) to manage our project code, we only need to add the source code of the $GOPATH/src directory. The contents of the bin and pkg directories do not require version control.
Suitable for individual developers
We know that the source code is stored in the src directory of GOPATH, so we can organize our code according to the figure below.
The current popular project structure
In the Go language, code files are also organized by packages, and we can reference other people's packages or publish our own packages, but in order to prevent conflicts between project names of different packages, we usually use the top-level domain name as the prefix of the package name, so we don't have to worry about project name conflicts.
Because not every individual developer has his or her own top-level domain name, it is now popular to use a personal github username to distinguish between different packages.
For example: both Zhang San and Li Si have a project called studygo, so the path of these two packages will be:
Import "github.com/zhangsan/studygo"
And
Import "github.com/lisi/studygo"
Later, when we download other people's packages from github, such as:
Go get github.com/jmoiron/sqlx
Then, this package will be downloaded to src/github.com/jmoiron/sqlx in our local GOPATH directory.
Suitable for enterprise developers Go development editor
Go uses UTF-8-encoded text files to store source code. In theory, any text editor can be used for Go language development. VS Code and Goland are recommended here. VS Code is Microsoft's open source editor, while Goland is a paid IDE from jetbrains.
We use VS Code plus plug-ins here as a development tool for the go language.
VS Code introduction
VS Code full name Visual Studio Code, Microsoft open source is a free modern lightweight code editor, support almost all the mainstream development language syntax highlighting, intelligent code completion, custom hotkeys, parenthesis matching, code snippets, code comparison Diff, GIT and other features, support plug-in extensions, support Win, Mac and Linux platform.
Although not as powerful as some IDE, it is adequate for our day-to-day Go development after adding the Go extension.
Download and install
Official download address of VS Code: https://code.visualstudio.com/Download
All three major platforms support it. Please choose the corresponding installation package according to your own computer platform.
The first Go program Hello World
Now let's create the first Go project, hello. Create the hello directory in the src directory under our GOPATH.
Create a main.go file in this directory:
1 package main / / declares the main package, indicating that it is currently an executable program 2 3 import "fmt" / / imports the built-in fmt package 4 5 func main () {/ / main function, which is the entrance to the program execution 6 fmt.Println ("Hello World!") / / prints Hello World! 7} on the terminal
Go build
Go build means to compile the source code into an executable file.
Execute under the hello directory:
Go build
Or execute the following command in another directory:
Go build hello
The go compiler will go to GOPATH's src directory to find the hello project you want to compile.
The compiled executable will be saved in the current directory where the compilation command is executed, and the windows platform will find the hello.exe executable in the current directory.
The hello.exe file can be executed directly on the terminal:
D:\ code\ go\ src\ hello > hello.exeHello World!
We can also use the-o parameter to specify the name of the compiled executable.
Go build-o heiheihei.exe
Go install
Go install means installation. It compiles the source code to get the executable file, and then moves the executable file to the bin directory of GOPATH. Because we have configured the bin directory under GOPATH in our environment variables, we can execute the executable directly anywhere.
Cross-platform compilation
By default, the executable files of our go build are executable files of the current operating system. What should I do if I want to compile an executable file under linux under windows?
You only need to specify the platform and processor architecture of the target operating system:
SET CGO_ENABLED=0 / / disable CGO SET GOOS=linux / / Target platform is linux SET GOARCH=amd64 / / Target processor Architecture is amd64
Then execute the go build command, and you get an executable that can run on the Linux platform.
Compile 64-bit executable programs on Linux and Windows platforms under Mac:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go buildCGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build
Compile 64-bit executable programs on Mac and Windows platforms under Linux:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go buildCGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build
Compile 64-bit executable programs on Mac platform under Windows:
SET CGO_ENABLED=0 SET GOOS=darwinSET GOARCH=amd64go build
Thank you for your reading. the above is the content of "the method of building Go language environment". After the study of this article, I believe you have a deeper understanding of the method of building Go language environment, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.