GoLand creates Gin project
Introduction
This article explains how GoLand creates a Gin project.
content
Configure the go locale
First of all, you need to configure the Go language environment. Read this article for details: Go language installation and its environment configuration
install git
You can visit https://git-scm.com/ to download related software.
Once downloaded, you can simply double-click to install it.
If you need to execute the go get command in the cmd window, you must first install the git environment.
set go agent
In order to solve the problem that github may not be accessible or the access speed may be slow when downloading the gin framework, we need to configure the public proxy image of go in the cmd window. Please execute the following command:
go env -w GOPROXY=https://goproxy.io,direct
as shown in the picture
With this set up, our go command will quickly pull code from the public proxy mirror.
Download the gin framework
Execute the command in the cmd window:
go get -u github.com/gin-gonic/gin
But it should be noted that the latest version of Gin must be downloaded from the terminal with the go.mod file.
After the download is complete, the downloaded related files can appear in the library directory of the previous GOPATH.
create project
Open the goland software and create a new project, as shown in the figure:
write under Environment: GOPROXY=https://goproxy.cn,direct
Type in terminal: go get -u github.com/gin-gonic/gin
Here are the packages of various Gin frameworks.
It should be noted that in the go language, it is not possible to name the project as go, otherwise an error will be reported in this go.mod.
So just need to recreate it
After running the command again, it was found that no error was reported.
Create a project to test it out
It should be noted that the above must be pack main
Test code:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello!欢迎来到GO世界!")
})
// 默认端口是8080,也可以指定端口 r.Run(":80")
router.Run()
}
run successfully
In the browser address bar, enter http://127.0.0.1:8080 to access.