Set up the GO development environment

Go to the Golang website

Click Download

Click on the blue file name to download and install according to your system.

After the installation is complete, run the Go version command to check whether the installation is successful

go version
Copy the code

Change the Go image to enable the Go mod

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
Copy the code

View the Go environment configuration

go env
Copy the code

Initialize the project

Open the Goland code editor

New project

Enter the project file name

Initialize the project

go mod init myblog-server
Copy the code

Enable integration as prompted

If go.mod is displayed in the directory, the go project is successfully initialized

About the Gin

  • Gin is a Golang microframework with elegant encapsulation, friendly API, and clear source code annotations. Gin is fast, flexible, and fault-tolerant
  • For Golang, web frameworks are far less dependent than, say, Python or Java. One’s ownnet/httpSimple enough and very good performance
  • Not only can framework development save time for many of the usual encapsulation, but it also helps with the team’s coding style and specification

Importing GIN Framework

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

Create the main.go file in the root directory

Enter the following code

package main

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

func main(a) {
  myServer := gin.Default()
  myServer.GET("/".func(c *gin.Context) {
    c.JSON(200, gin.H{"msg": "hello go server"})
  })
  err := myServer.Run(": 8080")
  iferr ! =nil {
    fmt.Println("Server startup failed!")}}Copy the code

Right click to run, or directly click the green triangle in front of the main function to run

Enter localhost:8080 in the browser address box

The above content indicates that the server has been started successfully.