Install the Gin framework

First you need to initialize the go project in the directory

go init

You can see that a file is generated go.mod, and then use the following command to install the gin framework

go get -u github.com/gin-gonic/gin

Develop a good habit to initialize the project before writing it

go mod init
go mod tidy

If you do not initialize the project, there will be no third-party library completion prompt, such as gin, and github.com/gin-gonic/ginthere will be a red wavy line after manual introduction.

If you initialize the project, you don't need to manually import it. You can directly use the gin framework to import it automatically. There will still be red wavy lines after the introduction. At this time, update the go mod tidyimported package file and there will be no red wavy lines.

Later, use the following command to introduce the dependent package into the vendor directory under the project directory.

go mod vendor

The following is what I wrote before. I didn’t understand it before. Now it’s written clearly above. You don’t need to read the following.

Sometimes the fully installed gin framework will not automatically complete the code when it is actually used, and there will be no code prompts, nor will it automatically import the gin package, and after introducing "github.com/gin-gonic/gin" itself, There are red wavy lines. Although it can run normally in the end, the experience and development efficiency are particularly poor. The desired effect should be as shown below

package main
func main(){
  r:=gin.d//写到这里应该是有代码补全提示,如下图所示
}

Code completion example

After pressing Enter, automatically import "github.com/gin-gonic/gin"

Automatically import examples

I have been struggling with this problem for a long time. I don’t know why, but when I installed the gin framework on my own computer, it directly included the code of the gin framework to prompt those functions. When the company’s computer was installed for the first time, it could be used without any messy operations, but later in order to familiarize myself with the installation method. I uninstalled it, and after reinstalling it, it was dead. I reinstalled it several times, but it didn't work. It felt outrageous. The environment directories configured on the two computers were the same. They were all installed using the previous steps. As a result, now I It will work, but not the company's computer. I searched online for a long time and tried many methods. Finally, I found a pretty good tutorial and successfully solved it. However, I still need to use the command to copy the dependencies to the vendor. Solution Refer to the Go Basics Series | 4. Environment Setup (Supplementary) - gomod I am confused , I wonder if there is anyone who can give me some answers. The following are examples of the steps of this method that I have done myself.

First, you need to manually import it for the first time import "github.com/gin-gonic/gin"
and then cd to the current project folder in the terminal and use the following command

go mod init 项目名
go mod tidy
go mod vendor

Take my example, before the above command, even if it is introduced, it will still be a red wavy line, and there will be no code completion prompt.
Insert image description here
Insert image description here
Execute go mod init hello for initialization, a go.mod will be generated in the directory,
Insert image description here
and a direct prompt of go mod tidy will be given.
Insert image description here
Enter it directly
Insert image description here
Insert image description here
and then use the go mod vendor command to copy the dependent third-party packages to the vendor directory. The vendor directory will be automatically generated. In the directory are the required dependent files. Then you can
Insert image description here
see the code prompts.
Insert image description here

Guess you like

Origin blog.csdn.net/sywdebug/article/details/132762447