Column address: Technology column

Meanwhile, welcome to follow my wechat official account AlwaysBeta, more exciting content waiting for you.

Download the Go installation package from golang.org/dl/.

Decompress the installation package:

tar -C /usr/local- XZF go1.11.4. Linux - amd64. Tar. GzCopy the code

Modify environment variables:

export PATH=$PATH:/usr/local/go/bin
Copy the code

At this point, Go is installed, to verify:

[root@7a7120c97a4f go]# go versionGo version go1.11.4 Linux/amd64Copy the code

Create a new project named test with the following directory structure:

test/
|-- install.sh
`-- src/
Copy the code

The install.sh file contains the following contents:

#! /usr/bin/env bash
if[!-f install.sh ]; then
    echo 'install must be run within its container folder'> 1 & 2exit 1
fi

CURDIR=`pwd`
OLDGOPATH="$GOPATH"
export GOPATH="$CURDIR"

gofmt -w src
go install test

export GOPATH="$OLDGOPATH"
echo 'finished'
Copy the code

The reason install.sh is added and GOPATH is not configured is to avoid adding a path to GOPATH when adding a Go project. This is useful when we need to create a temporary project for practice or testing.

Create two new programs under the SRC directory with the following structure:

test/
|-- install.sh
`-- src/
    |-- config
    |   `-- config.go
    `-- test
        `-- main.go
Copy the code

The program contents are as follows:

// config.go

package config

func LoadConfig(a){}Copy the code
// main.go

package main

import (
    "config"
    "fmt"
)

func main(a){
    config.LoadConfig()
    fmt.Println("Hello,GO!")}Copy the code

Then execute sh install.sh in the project root directory and take a look at the project directory to make it look like this:

test
|-- bin
|   `-- test
|-- install
|-- pkg
|   `-- linux_amd64
|       `-- config.a
`-- src
    |-- config
    |   `-- config.go
    `-- test
        `-- main.go
Copy the code

Config. a is generated after package config is compiled. Bin /test is the generated executable binary.

Run bin/test. The output is Hello,GO! .

A common development test process can be done in this way, so let’s install the Gin framework.

go get -u github.com/gin-gonic/gin
Copy the code

Modify main.go as follows:

package main

import (
	"config"
	"fmt"
	
	"github.com/gin-gonic/gin"
)

func main(a){
	config.LoadConfig()
	fmt.Println("Hello,GO1!")
	
	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
}

Copy the code

Simple and quick, just execute go run main.go and access it in your browser. If you see {“message”:”pong”}, your Web service has started successfully.

This article is just a simple example, and there is much more to explore.