Go Language (Engineering and package)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.