Go programming--variables, function export and initial capitalization

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

Go programming--variables, function export and initial capitalization

Identifiers can be exported to allow access from another package.

An exported identity is also met with the following conditions
1. The first character of the identifier name is a Unicode uppercase letter (the Unicode category "Lu");
2, the identifier is declared in the package block or is a field name or method name.


Example of importing a Gotest/even package into a program

Package Mainimport (    "even" "    FMT") func main () {    i:=even. I    FMT. Printf ("is%d even?" %v\n ", I, even. Even (i))}

Package Evenvar I int = 123 Func even (i int) bool {    return i%2==0}func odd (I int) bool {    return i%2!=0}

1. Local package even is imported here; 2. Import the official FMT package;3. call the function in the even package. The syntax for accessing a function in a package is <package> Function (), variable <package>. Var. In Go, when capitalize the first letter of a variable or functionThe time, The function is exported from the package (visible outside the package, or public ), so the function name is even. If you modify the 10th line of Main.go, use the non-guided out of the function even.odd:

Fmt. Printf ("is%d even?" %v\n ", I, even.odd (i))

Because of the use of private functions, a compilation error is obtained:

Main.go:10:cannot refer to unexported name even.odd

In summary: The name of the public function begins with a capital letter, and the name of the private function begins with a lowercase letter.

For structural bodies
Type S struct {    T1  //Field name is T1    *t2   //Field name is T2    p.t3    //Field name is T3    x, y int//Field name is X and y}
Similarly, an uppercase field can be exported, meaning that it can be read and written in other packages. Word The segment name begins with a lowercase letter that is private to the current package, and the function definition is similar. For T1, *t2 and other anonymous fields, the function is "if the struct S, contains an anonymous field T1, then the struct S has a T1 method." If the included anonymous field is *T2, then the struct S has a *t2 method. See another article http://blog.csdn.net/typ2004/article/details/41605083 (Golang anonymous field).


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.