Go core development study notes (six) - Identifier

Identifier

  1. Go language character sequence for a variety of variables, methods, functions, etc. used to be named identifier.
  2. Where everything can be a name is an identifier.

rule

  1. 26 case letters, numbers, underscores _
  2. The numbers do not begin with
  3. Strictly case-sensitive, Num and nUm not a variable.
  4. Identifiers can not contain illegal characters
  5. Do not use the keyword, which is kind of reserved identifiers, break, if it does not recommend non-key but is int, float the word.

Identifier naming convention:

  1. Package name: Keep package names and directory names remain the same.
  2. Variable names, function names, using the constant name hump method, like py.
  3. ★★ If the variable names, function names, constant names capitalized, other packages can be accessed (public), if you can only use lowercase first letter in this package (private), there is no similar java public, private, and original employed to control access to sensitive range.

How to reference other packages variables in the file name .go

  1. To $ GOPATH example, if set, then set the relative path on it.
  2. $ GOROOT or in the src folder directly to the establishment of variable folder, .go file written public variables.
  3. When public capital variable .go file must define a variable identifier
    such as:
    >> called file
    package varsImport          //新建一个文件夹,名字为varsImport
    
    var (                       //定义相关变量,只有首写字母为大写的,才可以被其他程序调用本文件的变量
    Name string = "durant"      //可调用
    name string = "james"       //不可调用
    Age int8 = 20               //可调用
    age int8 = 10               //不可调用
    )
    
    >> the calling file
    package main
    
    import "fmt"
    import "varsimport"            //调用了GOPATH目录下的
    
    func main() {
    fmt.Println(varsimport.Name)   //调用时候使用是 <包名>.<变量>,记得不关包下面xx.go的xx名字鸟事儿。
    }
    
  4. There are 36 pre-defined identifiers, including basic data types and system built-in functions (append, recover, panic ...), there are 25 reserved keyword, must be careful not to use the chaos.
Published 50 original articles · won praise 18 · views 4023

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/89600292