Creation of HelloWorld
Let's take MyMath as an example of how to write an app package and execute the following code:
CD $GOPATH/srcmkdir MyMath
Create a new file Sqrt.go with the following content:
Package Mymathfunc Sqrt (x float64) float64 {z: = 0.0for I: = 0; i <; i++ {z-= (z*z-x)/(2 * x)}return Z}
Note: It is generally recommended that the package name and directory name remain the same
At this point, the app package has been created, and the following is the installation of the app package, as in the next two ways:
1, just enter the corresponding application package directory, and then execute go install
, you can install the
2. Execute the following code in any directorygo install mymath
After the installation is complete, pass the command:
CD $GOPATH/pkg/${goos}_${goarch}
You can see the file mymath.a
At this point, the application package has been established, a program to invoke the application package, a new application package Mathapp
CD $GOPATH/srcmkdir mathappcd mathappvim main.go
The source code of Main.go is as follows:
Package Mainimport ("MyMath" "FMT") func main () {FMT. Printf ("Hello, World. ") SQRT (2) =%v\n ", MyMath. SQRT (2))}
Go to the app's directory and the Go build will generate a Mathapp executable that executes the following command:
./mathapp
You will get the following output:
Hello, world. SQRT (2) = 1.414213562373095