Go Language Pack and package reference

Source: Internet
Author: User

The go language package is very similar to the package in Java, both in the way the code is organized and in relation to the directory structure on disk.

In the go language, the package name is generally the directory name of the go code, but unlike Java, the package name is only one level in the go language, while in Java the package name is a multi-level catalog with points split.

In the go language, it is necessary to refer to the package with the GOPATH/SRC directory as the relative root directory, and then enter the following directory names at each level.

Example: Environment variable Gopath = ~/go

Package Hello has hello.go, the disk path where the package is located is:

~/go/src/golang_everyday/hello

In other words, the path to Hello.go is:

~/go/src/golang_everyday/hello/hello.go

When you reference the go file in another file, the code should be:

Import "Golang_everyday/hello"

If there are multiple Gopath, the go will be searched next to each gopath SRC until it is found.

Main is a special package name, similar to the Java main function, go executable program must be under the main packages, the main folder is not usually called main.

In the same package, multiple files are treated as a file by the go compiler, so the same global variables and functions cannot appear in multiple files, one exception being the init function, and the different files of the same package can refer to each other directly.

Go Language Pack and package reference

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.