In the go language, a package is a collection of functions and data that defines a package with the package whose name does not have to be the same as the file name, and then learns how to use the Go language package.
First, we create a stack package and then use a main program to invoke the method inside the package. This actually involves subcontracting and compiling the works in the go language. The go language is so configured for its engineering, such as
Where The package is the root directory of the project, we need to set the environment variable Gopath in advance, as follows
The bin directory and the PKG directory are automatically generated by Go compilation, and SRC is created manually. So the steps to create the project are roughly
(1) Enter the package directory, which is the project root directory
(2) Execute mkdir src, create a src source file storage directory
(3) Create a stack package in the source file directory, which is the mkdir stack
(4) Create a test package that tests the functionality of the stack, which is the main program in this package
(5) in src directory, execute go install stack, generate pkg folder outside SRC, linux_386 represents execution platform
(6) in the SRC directory, execute go install test, outside the SRC generated bin folder, which is the executable file.
The above is the Go language engineering construction steps and precautions, note must not forget the gopath settings .
If you need to see the go language-related environment variables, use the command go Env , as follows
Next, paste the code for the stack.go and test.go files.
Stack.go
Package Stackimport ("StrConv"//This packet provides a basic data type converted to a string, or converted from a string to a base data type) type Stack struct {i intdata [20]int}func (S *stack) Push (k int) {S.DATA[S.I] = Ks.i++}func (s *stack) Pop (ret int) {S.i--ret = S.data[s.i]}func (S *stack) string () string {var str stringfor I: = 0; i < s.i; i++ {str = str + "[" + StrConv. Itoa (i) + ":" + StrConv. Itoa (S.data[i]) + "]"}return str}
Test.go
Package Mainimport ("FMT" "stack") func main () {var st = new (stack. Stack) St. Push (1) St. Push (2) St. Push (3) fmt. Println (ST)}
This is the introduction of the Go Language project and the use of the package.
Go Language (Engineering and package)