Home  >  Article  >  Backend Development  >  Introduction to go mod modularity

Introduction to go mod modularity

尚
forward
2020-02-19 17:56:003143browse

Introduction to go mod modularity

Many languages ​​have special tools or libraries to manage dependency packages or third-party libraries in projects, such as maven for java and npm for javascript.

But for go, there is no built-in go module tool before v1.1. The project file needs to be run under GOPATH/src. This way of working is relatively mandatory and may reduce efficiency. .

After v1.1, go provides a built-in go mod tool. Through go mod, we can happily manage and upgrade go dependency packages.

The following is an introduction to go mod from the go Getting Started Tutorial column.

Go mod common commands

download    download modules to local cache
edit        edit go.mod from tools or scripts
graph       print module requirement graph
init        initialize new module in current directory
tidy        add missing and remove unused modules
vendor      make vendored copy of dependencies
verify      verify dependencies have expected content
why         explain why packages or modules are needed

Configure go mod in a new project

Create a new directory outside GOPATH and use go mod init xxx to initialize go.mod file. go.mod provides the name and version of the dependent library of the current project

Create new main.go and add the code

package main

import "github.com/astaxie/beego"

func main() {
	beego.Run()
}

If you are using the vscode editor, the go expansion will automatically Open the dependency in the file to download or you can run go run main.go to download the dependency package.

At this time the content in go.mod has been updated

module go-mod

go 1.13

require github.com/astaxie/beego v1.12.0

At the same time, the go.sum file will be generated in the project. This file records the current dependency tree

For golang.org The dependency package can be replaced with the dependency package in github.com by replacing

The beego project uses go mod

When the beego project generated through the bee tool is generated under GOPATH, beego needs to be The project is moved outside the GOPATH and go mod init xxx is generated to generate the go.mod file.

For more go language knowledge, please pay attention to the go language tutorial column on the php Chinese website.

The above is the detailed content of Introduction to go mod modularity. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete