Golang Basics-Referencing external functions

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

As shown below, the Main.go file functions if you want to invoke the specific implementation of the test () function in Func1.go in the Func folder in the Main.go sibling directory.
test |--func |--func1.go |--func2.go |--main.go
What you need to write in the Main.go file is
package mainimport( "test/func")func main(){ func.Test()}

One of the things to note is:

1, test project to Golang in the $gopath path.

For example $GOPATH=/home/data , the test file path is /homg/data/src/test then the path of the import reference in Main.go "test/func" . If the file path is /home/data/src/XXX/test so main.go, the path to the import reference is "XXX/test/func" . The external file referenced in import is visible according to the relative path of the $gopath.

$GOPATHThere will be three directories generated under the path src , pkg and bin . srcstore source code (e.g., .go .c .h .s etc.), pkg intermediate files generated at compile time (e.g.: .a ), bin executable files generated after compilation (for convenience, this directory can be added to the $PATH variable, If there are multiple GOPATH , then use ${GOPATH//://bin:}/bin the Add all Bin directory).

2, the called Func1.go file in the test () function, the first letter to uppercase.

Golang the first letter of the variable, method name is uppercase when the default can be called externally, the first letter is lowercase when the default cannot be called externally.

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.