In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to install go language in CentOS6.8". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to install go language in CentOS6.8.
Go is designed as a system programming language for giant central servers equipped with Web servers, storage clusters or similar purposes. For the field of high-performance distributed systems, Go language undoubtedly has higher development efficiency than most other languages. It provides massive support for parallelism, which is great for game server development.
I. installation and use of Go
1. Download the Go source code package
Https://storage.googleapis.com/golang/go1.6.3.linux-amd64.tar.gz
Upload to / usr/local/src directory
2. Compile and install Go to / usr/local
Tar zxvf go1.6.3.linux-amd64.tar.gz-C / usr/local/
# Note: you must use a root account or use sudo to extract the Go source code package
3. Set PATH environment variable and add / usr/local/go/bin to environment variable
Export PATH=$PATH:/usr/local/go/bin
4. Install to a custom location
The Go binaries are installed to / usr/local/go by default, but you can install the Go tool to a different location and define it yourself, as long as you set the correct environment variable.
For example, to install Go to the home directory, you must add an environment variable to $HOME/.profile
Export GOROOT=$HOME/goexport PATH=$PATH:$GOROOT/bin
Note: when installing Go to another directory, GOROOT must be set to the environment variable
5. Check whether the program is installed correctly
Check that a simple program is installed correctly by setting up a workspace and creating a simple program. Create a directory that contains your workspace, such as / data/work, and set the location to which the GOPATH environment variable points.
Export GOPATH=/data/work
# if / data/work does not exist, create a new one
Then, create a src/github.com/user/hello in your work. If you use github, you can use your own user name instead of user. Under the hello directory, create a new hello.go.
# cat hello.gopackage mainimport "fmt" func main {fmt.Printf ("hello,world!\ n")}
# compiling hello.go with go
Go install github.com/user/hello
# the above command says that a program called hello (or hello.exe) is put in your workspace, execute the following command, and you will get the output.
$GOPATH/bin/hellohello,world!
# when hello,world appears! Indicates that Go has been successfully installed and ready to work.
II. Introduction of Go work area
1. Overview of organization code
Go language programs usually keep all the code in a workspace.
The workspace contains many version control libraries (managed by Git).
Each repository contains one or more packages.
Each package consists of one or more source files in a directory.
The path to a package's directory determines its import path.
Note: as in other programming environments, each project has a separate workspace and the workspace is a closely linked version control library.
2. Introduction of the work area
The workspace is a directory hierarchy with three directories at its root:
Src contains Go source files
Pkg contains objects and packages
Bin contains executable commands Go tools to create source packages and install binaries to pkg and bin directories the src directory usually contains multiple version control libraries (such as Git or Mercurial) to track the development of one or more source packages. Here is an example of a good workspace:
Bin/hello # command executableoutyet # command executablepkg/linux_amd64/github.com/golang/example/stringutil.a # package objectsrc/github.com/golang/example/.git/ # Git repository metadatahello/hello.go # command sourceoutyet/main.go # command sourcemain_test.go # test sourcestringutil/reverse.go # package sourcereverse_test.go # test sourcegolang.org/x/image/.git/ # Git repository metadatabmp/reader.go # package sourcewriter.go # package source... (many more repositories and packages omitted)...
The property diagram above shows a workspace with two repositories (example and image), the example repository contains two commands (hello,outyet), and the image library contains the bmp package and several other packages.
A typical workspace contains multiple source libraries containing many software packages and commands. Most programmers keep all source code and dependencies in one workspace
3. GOPATH environment variable setting
The GOPATH environment variable specifies the location of the workspace. It is probably the only environment variable that needs to be set when the code is developed.
To begin, create a workspace directory and set the corresponding gopath. Your workspace can be located anywhere you like, but we will use / data/work in this document. Please note that this cannot be the same as your "Go installation" path.
Mkdir-p / data/workexport GOPATH=/data/work
for convenience. Add the bin of the workspace to PATH
Export PATH=$PATH:$GOPATH/bin
4. Import path
An import path is a string that uniquely identifies a package. The import path of a package corresponds to its location in the workspace or in the remote repository.
The short import path is given from the software package of the standard library. For your own package, you must choose a path that cannot conflict with the underlying path that will be added to the standard library or other external library in the future.
Note that you don't need to publish your code to a remote repository before you can build it. It's just a good habit to organize your code if you will publish it one day. In practice, you can choose any path name, as long as it is the only standard library and a larger de-ecosystem.
We will use github.com/user as our basic path. Create a directory in your workspace to keep the source code:
Mkdir-p $GOPATH/src/github.com/user
5. The first project
To compile and run a simple program, first select the path to a package (we will use github.com/user/hello) and create the corresponding package directory in your workspace:
Mkdir $GOPATH/src/github.com/user/hello
Create a file called hello.go, which was created above and skipped here.
Cd $GOPATH/src/github.com/user/hellogo install$GOPATH/bin/hello
Or:
Hello
If you are using a source control system, now is a good time to initialize a repository, add files, and commit your first changes. Third, this step is optional: you don't need to use source control to write code.
Cd $GOPATH/src/github.com/user/hellogit initInitialized empty Git repository in / data/work/src/github.com/user/hello/.git/git add hello.gogit commit-m "first commit" [master (root-commit) bbfb477] first commit
6 、 first library
Mkdir $GOPATH/src/github.com/user/stringutil
Next, create a file called reverse.go in the directory with the following:
/ / Package stringutil contains utility functions for working with strings.package stringutil// Reverse returns its argument string reversed rune-wise left to right.func Reverse (s string) string {r: = [] rune (s) for I, j: = 0, len (r)-1; i return string (r)}
Using go build to test the compilation of packages
$go build github.com/user/stringutil
If the current location source package directory, only need:
Go build
The above operation does not produce an output file. You must use go install to output packages and objects to the working pkg directory.
After confirming that the stringutil package is created, modify the original hello.go and use the stringutil package:
Package mainimport ("fmt"github.com/user/stringutil") func main () {fmt.Printf (stringutil.Reverse ("\ n! oG, olleH"))}
Whether you use the go installation package or binaries, all related dependencies are installed automatically. So when you install the hello program:
$go install github.com/user/hello
The corresponding stringutil package will be installed automatically.
Execute the new hello program and you can see that the message has been reversed
# helloHello, Go!
After completing the above, the workspace should be:
├── bin │ └── hello # command executable ├── pkg │ └── linux_amd64 # this will reflect your OS and architecture │ └── github.com │ └── user │ └── stringutil.a # package object └── src └── github.com └── user ├── hello │ └── hello.go # command source └── stringutil └── reverse.go # package source
Note: go install will put the library file stringutil.a under pkg/linux_amd64 (the directory structure is the same as the source code structure). In this way, the go command can find the corresponding package object directly, avoiding unnecessary repeated compilation. Linux_amd64 is designed to cross-compile according to the operating system and your system architecture.
All Go executables are statically linked together, so no associated package objects (libraries) are needed at run time.
7. Package command
All Go source code starts with the following statement:
Package name
Where name is the default name of the package reference, all files in a package must use the same package name, and the executable command must be main.
All package names under a binary file do not need to be unique, but the reference path must be unique
8. Testing
Go comes with a lightweight testing framework made up of go test and testing packages.
You can write a test that contains several TestXXX functions by creating a new xx_test.go. The test framework automatically executes these functions; if the function contains tError or t.Fail, the corresponding test is judged to fail.
Add a test file for stringutil, $GOPATH/src/github.com/user/stringutil/reverse_test.go, that contains the following:
Package stringutilimport "testing" func TestReverse (t * testing.T) {cases: = [] struct {in, want string} {{"Hello, world", "dlrow, olleH"}, {"Hello, World", "World, olleH"}, {","},} for _, c: = range cases {got: = Reverse (c.in) if got! = c.want {t.Errorf ("Reverse (% Q) =% Q, want% Q", c.in, got, c.want)}
# pass the go test test
# go test github.com/user/stringutilok github.com/user/stringutil 0.002s
# similarly, under the package folder, you can ignore the path and execute go test directly
[root@zabbix stringutil] # go testPASSok github.com/user/stringutil 0.002s
9. Remote package
The reference path of the package is used to describe how to obtain the source code of the package through the version control system. The go tool automatically fetches the package file from the remote code repository through the reference path. For example, the examples used in this article are also saved under github.com/golang/example. Go can obtain, generate and install the corresponding package directly through the url of the code repository of the package.
[root@zabbix ~] # go get github.com/golang/example/hello [root@zabbix ~] # $GOPATH/bin/helloHello, Go examples!
If the corresponding package does not exist in the workspace, go places the corresponding package under the workspace indicated by the GOPATH environment variable. (if the package already exists, go skips the code pull and executes go install directly)
After executing the go get command above, the result under the workspace folder is as follows:
The hello command on github relies on the stringutil library under the same repository, and hello.go uses the same path for import, so the go get command can directly find and install the corresponding dependency package.
Import "github.com/golang/example/stringutil"
# it's best to share Go packages in this way.
Thank you for your reading, the above is the content of "how to install go language in CentOS6.8". After the study of this article, I believe you have a deeper understanding of how to install go language in CentOS6.8, and the specific use needs to be verified in 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.