This is a creation in Article, where the information may have evolved or changed.
Gopath and Workspace
The core principle of the go language is to keep it simple, and in terms of project construction, the source code and the build script are put together, and the structure is fixed, just like maven.
Add a go after path (Download the unpacked Go package) bin, so that you can freely use the GO command
Gopath's system environment variables, and ava_home are not the same for Java, to allow GO commands and other related tools to find the Go workspace
Gopath D:\home\user\ext;d:\home\user\mygo
can have multiple, semicolon-separated. The colon is separated from UNIX.
The structure of each workspace
src
Contains Go source files,
pkg
Contains compiled package objects, and
bin
Contains executable commands
Create these three folders.
Then you can write the go source file in Src,
Command and then execute the Go Install command
For the Sqrt.go file its file path is D:\workspacego\src\example\newmath
D:\workspacego is Gopath.
SRC is the source directory
Example is the name of the project
Newmath This is the package declaration in the Code
Package Newmath
SQRT returns an approximation to the square root of X.
Func Sqrt (x float64) float64 {
This is a terrible implementation.
Real code should import "math" and use MATH.SQRT.
Z: = 0.0
For I: = 0; i < 1000; i++ {
Z-= (z*z-x)/(2 * x)
}
Return Z
}
And another file Hello.go D:\workspacego\src\example\hello
Its package is main, which represents his main function, which is the entry for this project.
Unlike the top package to save the file directory name, if it is main, the file name is consistent with the directory name
It will be converted to an execution command
Package Main
Import (
"Example/newmath"
"FMT"
)
Func Main () {
Fmt. Printf ("Hello, World.") SQRT (2) =%v\n ", Newmath. SQRT (2))
}
Go Install Example/hello
Compiling the installation command
bin/ Hello # command executablepkg/ linux_amd64/ example/ newmath.a # Package objectsrc/ example/ hello/ hello.go # command source newmath/ sqrt.go # package source
An execution command is generated under the bin and is hello.exe under window