An article teaches you how to use go language modules

Time:2021-10-17

An article teaches you how to use go language modules

preface

Go moudules is a package management tool of go. It is officially provided and reliable. The minimum go version requires 1.11 +.

It can be understood as Python virtual environment, Java Maven and PHP composer.

Its essence is to solve the problem of package management confusion.

Anyway, it’s done with it. There are only good things, not bad things.

Configuration (no pipe)

  1. Go111module = off # not applicable to modules function
  2. Go111module = off # using the molules function, you will not find dependent packages under gopath. Version 1.11 + is enabled by default
  3. Go111module = Auto: does golang #golang use the modules function

According to the above commands, it can be concluded that in version 1.11 +, go modules can be used by default without other settings.

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

When go modules is not used, the packages we download directly from go get will be placed in the% gopath% / SRC directory by default.

An article teaches you how to use go language modules

When go modules is enabled, packages will be downloaded to the% gopath% / PKG / mod folder.

An article teaches you how to use go language modules

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

Start using

Create a go.mod file

The go.mod file is equivalent to the requirement.txt file, which will put some required dependent packages.

Start using

I created a gomod demo folder.

An article teaches you how to use go language modules

And CMD enters this folder.

An article teaches you how to use go language modules

Execute the command go mod init gomod demo.

The later gomod demo can be written freely. It is recommended to be the same as the folder name.

An article teaches you how to use go language modules

After success, a go.mod file will be added to the folder to represent success.

An article teaches you how to use go language modules

I use GoLand to open this gomod demo folder.

An article teaches you how to use go language modules

Go.mod file description

  1. Module: module name.
  2. Require: dependent package list and version.
  3. Exclude: prohibit dependency package list (effective only when the current module is the main module).
  4. Replace: replace the dependent package list (effective only when the current module is the main module).

Of course, we don’t need to move these basically. They are modified through commands or IDE.

Simple use

For example, I need to use an 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

Execute the download command.

An article teaches you how to use go language modules

The go.mod file will automatically add a dependent package.

An article teaches you how to use go language modules

code:

  1. packagemain
  2.  
  3. import(
  4. “fmt”
  5. “github.com/huandu/xstrings”
  6. )
  7.  
  8. funcmain(){
  9. s: = “near hehedsl”
  10. center:=xstrings.Count(s,”h”)
  11. fmt.Println(center)
  12. }

Go connect to MySQL

The GitHub documentation is as follows.

An article teaches you how to use go language modules

Execute the download command go get – u github.com/go-sql-driver/mysql

An article teaches you how to use go language modules

The go.mod file is as follows.

An article teaches you how to use go language modules

code:

  1. packagemain
  2.  
  3. import(
  4. “database/sql”
  5. _”github.com/go-sql-driver/mysql”
  6. “time”
  7. )
  8.  
  9. funcmain(){
  10. //s: = “near hehedsl”
  11. //center:=xstrings.Count(s,”h”)
  12. //fmt.Println(center)
  13. db,err:=sql.Open(“mysql”,”user:[email protected]/dbname”)
  14. iferr!=nil{
  15. panic(err)
  16. }
  17. //See”Importantsettings”section.
  18. db.SetConnMaxLifetime(time.Minute*3)
  19. db.SetMaxOpenConns(10)
  20. db.SetMaxIdleConns(10)
  21. }

Possible exceptions

If you use the go get third-party package, GoLand cannot import it, or an error is reported, or there is no goModules

An article teaches you how to use go language modules

In the project directory, you can basically solve the problem by using go mod download. As long as there are go modules, it means there is no problem.

An article teaches you how to use go language modules

If there is still no go modules, set – > go – > go modules (VGO) in Golan

An article teaches you how to use go language modules

Go mod all commands

  1. Initialize a new module in the current folder and create a go.mod file: gomodinitname
  2. Pull the missing module and remove the unused module: gomodtidy
  3. Copy dependency to vendor: gomodvendor
  4. Download dependency: gomoddownload
  5. Test dependency: gomodverify
  6. Display module dependency graph: gomodgraph
  7. Explain why dependency is needed: gomodwhy
  8. Edit the go.mod file: goeidt
  9. View command list: gomod
  10. View the command help document: gohelpmod

summary

Above, we learned how to use go mod. If there is a task problem during the operation, remember to leave a message in the discussion area below, and we will solve the problem at the first time.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]