Export identifiers in Go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. A package consists of source files within a single directory. In such a directory, it is illegal to get files from different packages. The package statement that starts each source file in Go defines the packages that the file belongs to: "' gopackage foo ' >" is not a declaration that introduces a new identifier, so "foo" cannot be used later in the source file. The name of the package has a syntax similar to regular identifiers. All files that share the same package name form a package. In order to use identifiers from other packages, an import declaration is required: "Goimport" the string specified by "FMT" after the ' import ' keyword is called the import path. It needs to uniquely identify a package. Packages in the standard library use a shorter import path, but generic packages, such as ' Github.com/mlowicki/foo ', may look longer. In the form above, access to the exported identifier is done through the package name in the packages declaration. Therefore, in addition to identifying the files that belong to the package, it will also act as the default package name for the import declaration. It can be overwritten by an identifier before the path is imported: ' ' Goimport (f "FMT") func main () {f.println ("whatever")} ' > ' (Scope in Go] (https://medium.com/@ MLOWICKI/SCOPES-IN-GO-A6042BB4298C), the scope of the package name is the file block. Not all package identifiers can be accessed after the import. Only the exported identifiers can be accessed, and the export identifiers must follow two rules to access directly from other packages: * The first character of the identifier is an uppercase letter * Either the identifier is defined in the package block, or the field name or method name # # The identifier of the package block is defined in the package block meaning that it is defined outside any function, such as: ' ' gopackage libraryvar V = 1type S struct {Name String}type I interface {M ()} ' v,s and I can Used in a file with the appropriate import statement: "' Gopackage mainimport (" FMT "" Github.com/mlowicki/library ") func main () {s: = library. S{}fmt. PRINTLN (library. V, s)} ' # # # The field name of the field names that you export must also start with an uppercase letter to access from other packages: ' ' GopacKage Librarytype record struct {Name stringage int8}func Getrecord () record {return record{name: "Michał", Age:29}}packag E Mainimport ("FMT" "Github.com/mlowicki/library") func main () {record: = library. Getrecord () fmt. Println (record. Name} "" Above the code can work, but try to access the non-exported field ' age ' ... ' gofmt. Println (me.age) ' compile-time failure: ' ' record.age undefined (cannot refer to unexported field or method age) ' export struct in a library package, for example to record , but this does not change anything-that is, the struct type still does not export the age field. # # The exported ' method ' name is the same as the field name rule applies to ' method ': ' ' gopackage libraryimport ' FMT ' type Duck interface {quack () walk ()}type Record s Truct{}func (Record) Quack () {FMT. PRINTLN ("Quack")}func (Record) walk () {fmt. Println ("Walk")}func Getduck () Duck {return record{}}package mainimport ("Github.com/mlowicki/library") func main () { Duck: = library. Getduck () Duck. Quack () Record: = Library. Record{}record. Quack ()} ' output: ' bash>./bin/sandboxquackquack ' Call method ' walk ' is illegal: ' goduck.walk () ' Output: ' Bashduck.walk Undefined (cannot refer to unexported field or method walk) ' or: ' GorecOrd.walk () ' will report the following error at compile time: ' ' bashrecord.walk undefined (cannot refer to unexported field or method library. Record. "". Walk) ""

Via:https://medium.com/golangspec/exported-identifiers-in-go-518e93cc98af

Author: Michałłowicki Translator: Wentingrohwer proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

306 Reads

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.