[Little white version] The simplest goland package tutorial includes the use of custom packages

1. Hello World

The simplest tutorial needs to start with the simplest things:

  1. mkdir myapp
  2. cd myapp
  3. go mod init myapp// myapp is the main project name

This line of command will generate a go.mod file, which will record all package dependencies, an empty go.mod only has the project name and go version number.

  1. nano main.go :
package main

import "fmt"

func main() {
    
    
	fmt.Println("Hello World!")
}
  1. go run main.goRun directly or go build -o run.execompile and execute.

Two, import go standard library/package

Importing the standard library of the go language is very simple. For example, in the Hello World example above, we imported a standard library named fmt and called the Println function in this library: fmt.Println("Hello World!")


3. Import third-party packages

  1. nano main.go:
package main

import (
	"fmt"
	"github.com/kataras/iris/v12" // iris是一个开源的Web框架
)

func main() {
    
    
	fmt.Println("Hello World!")
	app := iris.New()
	fmt.Println(app.APIBuilder)
}

  1. How to install third-party packages?
    At this time, if you execute it directly, go build -o run.exeyou will get an error message that the package cannot be found:

no required module provides package github.com/kataras/iris; to add it:
go get github.com/kataras/iris/v12

The error message also reminds you that you need to use the go get command to get this package. Now enter:
go get github.com/kataras/iris/v12
Of course, for reasons that cannot be explained clearly, when you enter this line of command, basically there is no response. We also need to do this:
go env -w GOPROXY=https://goproxy.cn,direct
Then enter again:
go get github.com/kataras/iris/v12
the system will automatically download the files required by iris, execute again:
go build -o run.exe
everything is normal.

Now, if you open go.mod again, you will find that a bunch of package dependencies are automatically added, because the iris package itself also references a large number of other third-party packages, which will be recorded in go.mod.

So, where is the file of the iris package automatically downloaded by the system just now stored?
First of all, we need to know the value of the GOPATH system variable, which is used under Windows: , and used
echo %GOPATH%under Linux . After getting the value of this variable, open C:\Users\USERNAME\go\pkg and find that these packages are here.echo $GOPATH


Fourth, import the package created by yourself

  1. Create a subdirectory under the root of the main project: mkdir mypackage
  2. cd mypackage
  3. nano mypackage.go
package mypackage // 注意包名是我们自定义的,与文件夹名称一致

import (
	"fmt"
)

func Sayhi() {
    
     // 这里定义了一个Sayhi的函数,注意首字母一定要大写,否则外部无法访问,这和其他OOP语言的public和private类似
	fmt.Println("Hello I come from another Package")
}

  1. back to main projectnano main.go
package main

import (
	"fmt"
	"myapp/mypackage" // myapp是我们的主项目名,mypackage是我们自定义包名
)

func main() {
    
    
	mypackage.Sayhi() // 调用mypackage下的Sayhi函数
}

5. Postscript

1. About the particularity of package main
Any go project needs a package main and a func main() function, otherwise the program has no entry and cannot run. Suppose we now try to create a random package such as package myapp in the first line of main.go instead of using package main, and then build -o run.execompile it. Although the code can be compiled, it cannot be executed:

insert image description here


2. Function calls with the same package but different file names

For example, now there are 2 files: main.go and file.go

The content of file.go is:

package main

import "fmt"

func Sayhello() {
    
    
	fmt.Println("Hi, I come from file1")
}

The content of main.go is:

package main

func main() {
    
    
	Sayhello()
}

Although the function Sayhello is not stored in main.go , because the first lines of both files are package main, we can call the function in file.go normally without any declaration in main.go. The code stored in different files is aggregated by the same package name, and it can be simply regarded as a large file.

Guess you like

Origin blog.csdn.net/rockage/article/details/130780308