Golang Introduction and Environmental Installation
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance — up to 40 times faster. If you need smashing performance, get yourself some Gin.
Gin is a micro-framework developed by Golang, similar to Martinier’s API, with the emphasis on small size, ease of use, and much better performance, becausehttprouterThe performance of the is improved 40 times.
Preparation link
I. installation of Golang
Firstly, the installation package is selected according to the corresponding operating systemdownload,
I am using Centos 64-bit system here.
wget https://studygolang.com/dl/golang/go1.9.2.linux-amd64.tar.gz
tar -zxvf go1.9.2.linux-amd64.tar.gz
mv go/ /usr/local/
Configure /etc/profile
vi /etc/profile
Add environment variable GOROOT and GOBIN to PATH
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
Add the environment variable GOPATH (this can set the directory location according to the actual situation)
export GOPATH=/usr/local/go/path
After the configuration is completed, execute the command to make it effective.
source /etc/profile
Enter at the consolego version
If the version number is outputInstallation successful
Then everyone will have some doubts and entanglements.go
What is there in itself and what is the environment variable we have just set?
1、go
What is there in itself
First of all, when we decompress, we will get a file calledgo
, which includes allGo
Some language-related files, including many folders and files below, let’s briefly explain what the main folders do:
- Api: used to store data according to
Go
API Incremental List File for Version Order. The API mentioned here includes public variables, constants, functions, etc. These API delta list files are used forGo
Language API check - Bin: used to store main standard command files (executable files), including
go
、godoc
、gofmt
- Blog: used to store all articles in the official blog.
- Doc: A program document in HTML format for storing standard libraries. We can pass.
godoc
Command to start a Web program to display these documents - Lib: Used to store some special library files
- Misc: Instructions and tools for storing some auxiliary classes
- Pkg: Used for storage and installation
Go
All archived files after the standard library (with.a
The end of the file). Note that you will find one of them namedlinux_amd64
We call it platform-related directory. The names of such folders are combined by the names of the corresponding operating systems and computing architectures. viago install
Orders,Go
The program will be compiled into platform-related archive files and stored in it. - Src: for storage
Go
Self,Go
Standard tools and all source files of the standard library - Test: Stored for testing and verification
Go
All relevant documents of itself
2. What is the environment variable just set
- GOROOT:
Go
The root directory of - GOPATH: user workspace
- Add $ gorot/bin under PATH:
Go
Thebin
The executable file will be stored next time. We can use it directly on the command line by adding it to PATH.
What is the working area?
This is inGo
China is a very important concept. Under normal circumstances,Go
The source code files must be placed in the workspace, that is, all the project codes we write must be placed in the workspace we set up, although this is not necessary for command source files. But most of us are in the former situation. The workspace is actually a directory corresponding to a specific project. It should contain 3 subdirectories:src
Directory,pkg
Directory,bin
Directory
- Src: Used to organize and save Go source files in the form of code packages
- Pkg: Used for storage and passage
go install
The archive file (.file ending in a) of the code package after the command is installed. - Bin: Similar to pkg directory, it is passed through
go install
After the command is installed, save the executable file generated by the Go command source file.
4. What is a command source file?
If a source file is declared to belong tomain
Code package, and the file code contains no parameter declaration and result declarationmain
Function, it is the command source file. Command source files can be passed throughgo run
The command starts the operation directly.
II. Installation of Govendor
If using go1.5, ensure GO15VENDOREXPERIMENT=1 is set.
Perform the installation at the command line
go get -u github.com/kardianos/govendor
Wait for a while, after the installation is successful.
wecd /usr/local/go/path
(the third-party dependency package will be installed in the first directory of GOPATH by default) directory.
carry outls
, which can be seen in the workspacebin
、pkg
、src
Three catalogues. This is what we said in the previous section.Working area of!
So, where is the govendor we installed?
The answer is in the workspace, and the generated code package is probably like this. What we need is a compiled executable file, which can be found in/usr/local/go/path/bin
China.
path/
├── bin
│ └── govendor
├── pkg
│ └── linux_amd64
│ └── github.com
│ └── kardianos
│ └── govendor
│ ├── ...
└── src
└── github.com
└── kardianos
└── govendor
├── ...
You still remember that we used to work on environmental variablesPATH
GOBIN is set in.
What we have to do now is to put the work areabin
Executable files in directorygovendor
To move past, or you can add the BIN directory of $GOPATH to the environment variable.
That way, it can be executed directly on the command line.govendor
The
mv /usr/local/go/path/bin/govendor /usr/local/go/bin/
After the move is successful, execute it at the command line.govendor -version
, if there is a version number, then success
#govendor -version
$ v1.0.9
Why do you single out a section heregovendor
?
You can think about it, although we develop and utilize it locally$GOPATH
There seems to be no problem in installing a third-party dependency package and then using it.
However, there are problems in actual multi-person cooperation and deployment.
- Every new comer needs to
go get
Many times - The version pulled down may also be different.
- Online deployment is even more troublesome
So we are simply using it here.govendor
To solve this problem, at the end of the project, you just need togovendor init
Againgovendor add +external
We can happily put all the dependency packages into the project.vendor
In the catalog, it can be uploaded to your version database together, isn’t it convenient?
Of course, at present there are more than a dozen package management tools recommended by the government. You can take a look at them properly. This is beyond the scope of this article.
Iii. installation of Gin
Perform the installation at the command line
go get -u github.com/gin-gonic/gin
Check/usr/local/go/path
Does it exist ingin
The code package for
4. Test whether Gin is installed successfully
Write atest.go
File
package main
import "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
}
carry outtest.go
go run test.go
Access $HOST:8080/ping, if returned{"message":"pong"}
Then correct
curl 127.0.0.1:8080/ping
So far, our environment installation has been basically completed:)
Specificgin
The introduction of fromSerial 2At the beginning, we will explain what Demo involves.Knowledge point!
References
This series of sample codes
This series of catalogues
- Serial One Golang Introduction and Environmental Installation
- Serialized 2 to build Blog API’s (1)
- Serial 3 to build Blog API’s (2)
- Serial 4 to build Blog API’s (3)
- Serial 5 Use JWT for Identity Verification
- Serial 6 Write a Simple File Log
- Serial Seven Golang Gracefully Restart HTTP Service
- Serial 8 Add Swagger to It
- Serial 9 Deploying Golang Applications to Docker
- Serial Ten Customized GORM Callbacks
- Serial Eleven Cron Scheduled Tasks
- Serial 12 Optimizing Configuration Structure and Realizing Picture Upload
- Serialization 13 Optimize Your Application Structure and Implement Redis Cache
- Serial 14 Realize Export and Import into Excel
- Serial 15 Generate Two-dimensional Code and Merge Posters
- Serial 16 Draw Text on Pictures
- Serial 17 Deploying Go Applications with Nginx
- Cross-compilation of safari Golang
- Please get started with Makefile
Relevant documents
- Gin
- Gin Web Framework
- Go concurrent programming actual combat
- govendor