In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand modularization of Go language". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
environment variable
The first step in learning go is, of course, installation and environment variables. Since I am macos, I can install it successfully by running brew install go directly, or download the corresponding binary package from the official website (https://golang.google.cn/).
After successful installation, you need to configure the following environment variables:
GOROOT: Go language installation path;
GOBIN: Go executable file path, usually "$GOROOT/bin";
GOPATH: work directory, multiple can be set, each project can be set a separate GOPATH;
GOPATH
In GoLand, we can set multiple GOPATH in Preferences and divide GOPATH into global and local.
GOPATH was originally used for module management, and there are three directories in each GOPATH:
src: Used to store source code;
pkg: used to store compiled.a(archive) static library files;
bin: Used to store binary files that can be directly run after compilation;
The src folder, which is normally set as a working directory, needs to be created manually, and the other two directories are automatically generated after compilation.
Next, we create a new directory ~/Code/goland/go-story and set it as a working directory.
export GOPATH="~/Code/goland/go-story"
Then create a src folder in the current directory, create a new hello directory, and create a main.go file in the hello directory.
In the hello/main.go file, write the following code:
package main import ( "flag" "fmt" ) var name string func init() { flag.StringVar(&name, "name", "everyone", "The greeting object. ") } func main() { flag.Parse() //parse command line argument fmt.Printf("\nHello %s\n", name) }
flag library is a built-in module of go, similar to the commander library of node, and the result after running is as follows:
Here we introduce a library that makes command-line output richer in color: colourize, similar to chalk in node. Install dependencies by issuing the following command:
go get github.com/TreyBastian/colourize
After running, we can see that a pkg directory is automatically created in the workspace. The newly generated directory is the colourize library file. At the same time, a github.com directory is also newly created in the src directory to put the colourize source code.
The go get command can simply be interpreted as npm install. Next you can introduce dependencies in hello/main.go.
package main import ( "flag" "fmt" "github.com/TreyBastian/colourize" ) var name string func init() { flag.StringVar(&name, "name", "everyone", "The greeting object. ") } func hello(name string) { fmt.Printf(colourize.Colourize("\nHello %s\n", colourize.Blue), name) } func main() { flag.Parse() hello(name) }
Run hello/main.go to see blue text on the command line.
By default, go depends on the loading mechanism:
src directory under $GOROOT
src directory under $GOPATH
Go Vendor
The problem with this approach is that there is no way to do good version management, and multiple dependencies are scattered in the $GOPATH/src directory, which may cause a lot of troublesome problems.
For example, I now have two projects under GOPATH: go-blog, go-stroy, these two projects have different dependencies, scattered in the github.com directory, this time in the end should I add the entire github.com directory to the repository?
Go introduced the vendor mechanism in version 1.5, where dependencies can be stored in each project directory through the vendor directory, similar to the node_modules directory in node.
To use go vendor, you need to install the govendor module first.
go get govendor
Then run the following command in the project directory.
cd ~/Code/gland/go-story/src/hello govendor init govendor add github.com/TreyBastian/colourize
As you can see, a new vendor directory has been created under the hello project, and colourize has also been copied to this directory.
And govendor creates a vendor.json file to manage dependencies.
With a go vendor, the order in which dependencies are loaded is as follows:
Vendor directory under project directory
The vendor directory one level above the project directory
Keep going up... PS. Similar to node_modules)
Vendor directory under $GOPATH
src directory under $GOROOT
src directory under $GOPATH
configuration switch
One thing to note is that in go 1.5, go vendor is not enabled by default, and environment variables need to be manually configured:
export GO15VENDOREXPERIMENT=1
In go 1.6, go vendor has been changed to default.
Go Modules
Although version 1.5 introduced go vendor, it did not solve the fundamental problem, but the dependent lookup did not support the vendor directory, the vendor directory still needs some third-party libraries (govendor, godep, glide) to manage, and still depends on the GOPATH environment variable.
In order to solve these problems, the official finally in version 1.11, experimental built-in module management capabilities (version 1.12 officially opened): go mod.
When using go mod, we don't need GOPATH, so we need to clean up GOPATH, adjust the directory structure, move go-story/hello/main.go directly to go-story/main.go, and then delete src and pkg directories.
#initialize go modules go mod init [pkg-name]
At this point, a go.mod file is generated in the directory.
Check its contents and find that it declares the version number of go and the name of the current module.
Then we install dependencies (no matter how dependencies are managed, the installation method remains the same):
go get github.com/TreyBastian/colourize
go.mod, the added dependencies are written, along with the version number, and the module is installed in GOPATH. Since we removed GOPATH earlier, this will install GOPATH at its default value (~/go/).
"How to understand modularity in Go" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.