Home >Backend Development >Golang >An article teaches you how to use Go language Modules

An article teaches you how to use Go language Modules

Go语言进阶学习
Go语言进阶学习forward
2023-07-20 15:55:061497browse
Preface

go moudules is a package management tool for Go. It is officially provided and is relatively reliable. , the minimum Go version required is 1.11.

can be understood as Python’s virtual environment, or Java’s maven, or PHP’s composer.

The essential problem it solves is the problem of package management confusion.

Anyway, just use it and that’s it. There are only good things and no bad things.


##Configuration (don’t worry)

GO111MODULE=off # 不适用modules功能
GO111MODULE=off #使用molules功能,不会去GOPATH下找依赖包, 1.11+版本默认开启
GO111MODULE=auto:Golang # Golang自己简则是不是使用modules功能

Based on the above command, it can be concluded that in version 1.11, go modules can be used by default, and no other settings are required.


What is the difference between it and the traditional direct go get

When go modules are not used, we directlygo get The downloaded package will be placed in the %GOPATH%/src directory by default.

An article teaches you how to use Go language Modules

After enabling go modules, the packages will be downloaded to %GOPATH%/pkg/mod folder.

An article teaches you how to use Go language Modules

And it will have a version number, which basically completely solves the dependency problem.


Get started

Create go.mod file

go.mod# The ## file is equivalent to the requirement.txt file, which will contain some required dependency packages.


Get started

I created agomod-demo folder.

An article teaches you how to use Go language Modules

andCMDenter this file Clip inside.

An article teaches you how to use Go language Modules

Execute the command go mod init gomod-demo.

The following gomod-demo can be written casually. It is recommended that it be the same as the folder name.

An article teaches you how to use Go language Modules

After success, there will be one more #go.modFile, represents success.

An article teaches you how to use Go language Modules

##I used Goland to open thisgomod-demo folder.

An article teaches you how to use Go language Modules

go.mod file description

  1. module: module name.

  2. require: Dependency package list and version.

  3. exclude: prohibits dependent package list (only takes effect when the current module is the main module).

  4. replace: Replace the dependent package list (only takes effect when the current module is the main module).

Of course, we don’t need to touch these basics, they are all modified through commands or IDE.


##Simple use

For example, I need to use a xstrings package.

github address:https://github.com/huandu/xstrings.

You need to execute this command to install this package.

An article teaches you how to use Go language Modules

执行下载命令。

An article teaches you how to use Go language Modules

go.mod文件会自动添加一个依赖包。

An article teaches you how to use Go language Modules

代码:

package main

import (
    "fmt"
    "github.com/huandu/xstrings"
)

func main() {
    s := "heheh ADSL附近"
    center := xstrings.Count(s, "h")
    fmt.Println(center)
}

go连接Mysql

github文档如下。

An article teaches you how to use Go language Modules

执行下载命令 go get -u github.com/go-sql-driver/mysql

An article teaches you how to use Go language Modules

go.mod文件如下。

An article teaches you how to use Go language Modules

代码:

package main

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "time"
)

func main() {
    //s := "heheh ADSL附近"
    //center := xstrings.Count(s, "h")
    //fmt.Println(center)
    db, err := sql.Open("mysql", "user:password@/dbname")
    if err != nil {
        panic(err)
}
    // See "Important settings" section.
    db.SetConnMaxLifetime(time.Minute * 3)
    db.SetMaxOpenConns(10)
    db.SetMaxIdleConns(10)
}

可能出现的异常

如果使用go get 第三方包时,goland无法导入,或者报错,或者没有Go Modules

An article teaches you how to use Go language Modules

项目目录下,使用go mod download下载一下,基本可以解决问题,只要有Go Modules,就代表没问题。

An article teaches you how to use Go language Modules

如果还是没有Go Modules,在Goland中settings->Go->GoModules(vgo)

An article teaches you how to use Go language Modules

go mod所有命令

在当前文件夹下初始化一个新的 module,创建 go.mod 文件:go mod init name
拉取缺少的模块,移除不用的模块:go mod tidy
将依赖复制到 vendor 下:go mod vendor
下载依赖:go mod download
检验依赖:go mod verify
显示模块依赖图:go mod graph
解释为什么需要依赖:go mod why
编辑 go.mod 文件:go eidt
查看命令列表:go mod
查看命令帮助文档:go help mod

The above is the detailed content of An article teaches you how to use Go language Modules. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete