This is a creation in Article, where the information may have evolved or changed.
Golang Introduction and Environment installation
Gin is a HTTP web framework written in Go (Golang). It features a martini-like API with much better performance – up to the times faster. If you need smashing performance, get yourself some Gin.
Gin is a micro-framework developed with Golang, similar to the Martinier API, with a focus on compactness, ease of use, performance, and a 40 times-fold improvement in httprouter performance.
Preparation session
First, install the Golang
First, choose the installation package download according to the corresponding operating system,
I'm using a CentOS 64-bit system here.
wget https://studygolang.com/dl/golang/go1.9.2.linux-amd64.tar.gztar -zxvf go1.9.2.linux-amd64.tar.gzmv go/ /usr/local/
Configure/etc/profile
vi /etc/profile
Add environment variable Goroot and add Gobin to Path
export GOROOT=/usr/local/goexport PATH=$PATH:$GOROOT/bin
Add environment variable Gopath (this can set the directory location according to the actual situation)
export GOPATH=/usr/local/go/path
After the configuration is complete, execute the command to make it effective
source /etc/profile
In console input go version
, if the output version number is installed successfully
Then there will be some questions, what is the entanglement go
itself, what is the environment variable we just set?
go
There's something in itself.
First of all, we will get a folder named after the decompression, go
including Go
some of the language-related files, which contains a lot of folders and files, we can briefly explain the main folder in the Act:
- API: Used to store
Go
API Delta list files in accordance with the version order. The API described here contains exposed variables, constants, functions, and so on. These API delta list files are used for Go
language API checking
- Bin: For storing the main standard command file (executable file), including,,
go
godoc
gofmt
- Blog: used to store all the articles in the official blog
- Doc: The HTML-formatted program document that holds the standard library. We can
godoc
start a Web program by command to show these documents
- LIB: For storing some special library files
- Misc: instructions and tools for storing some auxiliary classes
- PKG: Used to store
Go
all archive files (end files) after installing the standard library .a
. Note that you will find a folder in which there is a name linux_amd64
, which we call the platform-related directory. The names of such folders are combined by the corresponding operating system and the name of the compute schema. By go install
Command, the Go
program will be compiled into a platform-related archive file to be stored in it
- SRC:
Go
All source files used to store themselves, Go
standard tools, and standard libraries
- Test: Store all relevant files for testing drink verification
Go
itself
- What is the environment variable you just set
- Goroot:
Go
The root directory
- Gopath: User Workspace
- Add $goroot/bin under Path:
Go
The bin
following will be stored in the executable file, we put him into the path can be directly on the command line to use
- What is a work area?
This Go
is a very important concept, in general, the Go
source file must be placed in the workspace, that is, we write the project code must be placed in the workspace we set, although for the command source files, this is not necessary. But most of us are in the former case. The workspace is actually a directory for a specific project, which should contain 3 subdirectories: directory, directory src
pkg
, bin
directory
- SRC: Used to organize and save go source files in a code package
- Pkg: an
go install
archive file (a file ending with a. A) that is used to store the code package after the command is installed.
- Bin: Similar to the PKG directory, the
go install
executable file generated by the Go command source file is saved after the installation is completed by command
- What is the command source file?
If a source file is declared as belonging to a main
code package, and the file code contains a function that does not have a parameter declaration to drink the result declaration main
, then it is the command source file. Command source files can be go run
run directly by command
Second, installation Govendor
If using go1.5, ensure Go15vendorexperiment=1 is set.
Performing the installation at the command line
go get -u github.com/kardianos/govendor
Wait a while after the installation is successful.
We cd /usr/local/go/path
(third-party dependent packages, which are installed by default in the first directory of Gopath),
Execute ls
, you can see bin
, pkg
three directories in the workspace src
. This is what we said in the above section of the workspace !
So, where did the Govendor we installed go?
The answer is in the workspace, and the resulting code package is probably the case. All we need is a compiled executable file, in /usr/local/go/path/bin
.
path/├── bin│ └── govendor├── pkg│ └── linux_amd64│ └── github.com│ └── kardianos│ └── govendor│ ├── ...└── src └── github.com └── kardianos └── govendor ├── ...
We remember that we previously set the Gobin in the environment variable PATH
,
All we have to do now is to move the executable file in the directory in the workspace bin
govendor
, or you can add the $gopath Bin directory to the environment variable.
So you can execute directly on the command line govendor
.
mv /usr/local/go/path/bin/govendor /usr/local/go/bin/
After the move succeeds, executes at the command line govendor -version
and, if the version number appears, succeeds
#govendor -version$ v1.0.9
Why do you pick out a section here alone govendor
?
You can think about it, although we developed it locally, using the $GOPATH
reach to install a third-party dependency package and then go to use him, there seems to be no problem.
But there are problems in the actual multi-person collaboration and deployment.
- Every new person has to be a
go get
lot of times.
- The version you pulled down may be different.
- More trouble with online deployment
So we are in this simple use govendor
to solve this problem, at the end of this project, you just can be flattered to put the govendor init
govendor add +external
dependency package into the project vendor
directory, you can put it together in your version of the library, is not very convenient.
Of course, at present, the official recommended package management tools There are more than 10, we can properly inspect, this is not in the scope of this article.
Third, installation gin
Performing the installation at the command line
go get -u github.com/gin-gonic/gin
Check /usr/local/go/path
if gin
a code package exists
Iv. test whether the gin is installed successfully
Write a test.go
file
package mainimport "github.com/gin-gonic/gin"func main() { r := gin.Default() r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong", }) }) r.Run() // listen and serve on 0.0.0.0:8080}
Performtest.go
go run test.go
Access $host:8080/ping, if return {"message":"pong"}
is correct
curl 127.0.0.1:8080/ping
At this point, our environment installation is basically complete:)
Reference
Sample code for this series
Related documents
- Gin
- Gin Web Framework
- Go concurrent Programming combat
- Govendor