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).