Gin Practice Serial One Golang Introduction and Environmental Installation

  golang

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 versionIf the version number is outputInstallation successful

Then everyone will have some doubts and entanglements.goWhat is there in itself and what is the environment variable we have just set?

1、goWhat is there in itself

First of all, when we decompress, we will get a file calledgo, which includes allGoSome language-related files, including many folders and files below, let’s briefly explain what the main folders do:

  • Api: used to store data according toGoAPI Incremental List File for Version Order. The API mentioned here includes public variables, constants, functions, etc. These API delta list files are used forGoLanguage API check
  • Bin: used to store main standard command files (executable files), includinggogodocgofmt
  • Blog: used to store all articles in the official blog.
  • Doc: A program document in HTML format for storing standard libraries. We can pass.godocCommand 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 installationGoAll archived files after the standard library (with.aThe end of the file). Note that you will find one of them namedlinux_amd64We call it platform-related directory. The names of such folders are combined by the names of the corresponding operating systems and computing architectures. viago installOrders,GoThe program will be compiled into platform-related archive files and stored in it.
  • Src: for storageGoSelf,GoStandard tools and all source files of the standard library
  • Test: Stored for testing and verificationGoAll relevant documents of itself

2. What is the environment variable just set

  • GOROOT:GoThe root directory of
  • GOPATH: user workspace
  • Add $ gorot/bin under PATH:GoThebinThe 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 inGoChina is a very important concept. Under normal circumstances,GoThe 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:srcDirectory,pkgDirectory,binDirectory

  • Src: Used to organize and save Go source files in the form of code packages
  • Pkg: Used for storage and passagego installThe archive file (.file ending in a) of the code package after the command is installed.
  • Bin: Similar to pkg directory, it is passed throughgo installAfter 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 tomainCode package, and the file code contains no parameter declaration and result declarationmainFunction, it is the command source file. Command source files can be passed throughgo runThe 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 workspacebinpkgsrcThree 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/binChina.

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 variablesPATHGOBIN is set in.

What we have to do now is to put the work areabinExecutable files in directorygovendorTo 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.govendorThe

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$GOPATHThere 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 togo getMany times
  • The version pulled down may also be different.
  • Online deployment is even more troublesome

So we are simply using it here.govendorTo solve this problem, at the end of the project, you just need togovendor initAgaingovendor add +externalWe can happily put all the dependency packages into the project.vendorIn 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/pathDoes it exist inginThe code package for

4. Test whether Gin is installed successfully

Write atest.goFile

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:)

SpecificginThe introduction of fromSerial 2At the beginning, we will explain what Demo involves.Knowledge point!

References

This series of sample codes

This series of catalogues

Relevant documents