What Golang: Package Practice

Source: Internet
Author: User

In the Golang package is a confusing concept, especially the package can also be different from folder name, it makes me disgusting.

Best practices for the Golang package:

Package is folder.

Package name is folder name.

Package path is folder path.

Believe in Jesus and live forever!


1. There is a different package in the same folder, compile error:

D:/go/bin/go.exe build [E:/CGSS/SRC/PKG01]

Can ' t load package:package pkg01:found packages PKG01 (pkg01.go) and pkg012 (Pkg02.go) in E:\CGSS\SRC\PKG01

If multiple package is present in the same folder, the load fails. Even if it's main, it's the same.

D:/go/bin/go.exe build [E:/cgss/src/test]

Can ' t load package:package test:found packages main (cgss.go) and FILE01 (File01.go) in E:\cgss\src\test

2. Folder name differs from package name (that is, package path is different from package name), you need to use

The "Import alias path" syntax, which is:

Import <package_name> <package_path>

Otherwise compile error: Package name is not a legal identifier.

D:/go/bin/go.exe build [E:/cgss/src/test]

Can ' t load package:package test:found packages main (cgss.go) and FILE01 (File01.go) in E:\cgss\src\test

Package Main
Import "FMT" Import pkg012 "Pkg01"
Func Main () {FMT. Println (pkg012. Pkg_name)}

What Golang: Package Practice

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.