简体   繁体   中英

Golang how to install lib with go mod command

I searched some article, all they used this why:

$go mod init project_name

Then created main.go and coded these:

package main

import "github.com/gin-gonic/gin"

func main() {
 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 (for windows "localhost:8080")
}

then go run main.go

but in the fact, I can't remember the lib's full name like "github.com/gin-gonic/gin" , maybe I just remember gin .

So is there some way to install lib like python pip tool or go get like go mod install gin


PS:

because that I am using goland as my IDE, under the GOPATH, I can get the code tips, but under the go mod, I couldn't get it, Then I have started the question of this.

... but in the fact, I can't remember the lib's full name like "github.com/gin-gonic/gin", maybe I just remember gin.

So is there some way to install lib like python pip tool or go get like go mod install gin[?]

⛔️ No.

If you don't know full name of the package you cann't get it.

You can try to use IDE's tips ( GoLand as example IDE) to find a package without <domain>/<owner>/... , import full package name and use

go mod tidy

to modify go.mod file, but IDE will try to find package in you local cache.

Checkout https://blog.golang.org/using-go-modules

The go module name is unique, so normally starts with domain name (unless it is a build-in module).

So just copy your import string's and run go get you will get it. eg

go get github.com/gin-gonic/gin

To update a module, use -u :

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

I don't think so because some project have the same name, golang use project path system for your project. Usually yourhost/author/projectname, try googling your lib you try to remember with golang keyword eg gin golang. Most of the time you will find it.

So is there some way to install lib like python pip tool or go get like go mod install gin [?]

No. That is not how Go works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM