Go custom package settings and import operations

Time:2021-8-30

Package import record

First, you need to know two values

$GOROOT

$GOPATH

The second value can be set to multiple values, and a colon is used between the two values: to make a connection.

When the system looks for a package, it will look for it in the SRC under the corresponding path. For example, I create a new folder HelloWorld in the SRC directory of the path directory, where there is a file printhelloworld.go.

The corresponding code is:

package HelloWorld
import "fmt"
func PrintHelloWorld(){
	fmt.Print("Hello World\r\n")
	printILoveYou()
}
//Note that the lowercase printiloveyou cannot be called externally, but only internally
func printILoveYou()  {
	fmt.Print("I love you\r\n")
}

You can directly create a new main package under the external and non path directory. Here, you can declare the function of the main package, which is actually the starting point of the project. For example, the package we created above can actually be regarded as an auxiliary package. All the code written in it is functions, either for internal calls or external calls, and the main package is the entry of a project, As long as you associate the main package with the project, you won’t worry about how many main packages I need.

There can only be one main package in a directory. The syntax is as follows

Add: go language cannot import the package written by itself. Gopath plays a role in go language

Try go language for the first time. After configuring gopath according to the requirements of the document, I don’t know what to do…

After writing the first demo, I found that I couldn’t write my own package, and then turned back to see gopath

The structure is as follows

Bin is the generated executable

PKG is a compiled file

SRC is the project source file

Before Java projects, you would set a working path directory. In fact, this SRC is the same as this. It is the place where you put your project source code. You can’t import it into the package before, because the project is placed under a self built working path

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.

Recommended Today

Computer system 6-> group and Architecture 3 | MIPS instruction set (middle) | MIPS assembly instructions and machine representation

PreviousComputer system 5-> group count and Architecture 2 | MIPS instruction set (I) | instruction systemFrom the top level, it explains what features and working principles an instruction set / instruction system should have. This article focuses on the MIPS instruction set (mips32) to see what its assembly statements and machine languages look like. reference […]