GoLand creates Gin project

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
insert image description here

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.
insert image description here

After the download is complete, the downloaded related files can appear in the library directory of the previous GOPATH.

insert image description here

create project

Open the goland software and create a new project, as shown in the figure:
write under Environment: GOPROXY=https://goproxy.cn,direct
insert image description here

Type in terminal: go get -u github.com/gin-gonic/gin
insert image description here

Here are the packages of various Gin frameworks.
insert image description here
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.
insert image description here

So just need to recreate it
insert image description here

After running the command again, it was found that no error was reported.
insert image description here

Create a project to test it out
insert image description here

It should be noted that the above must be pack main
insert image description here

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
insert image description here

In the browser address bar, enter http://127.0.0.1:8080 to access.
insert image description here

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/130759680