Packages, variables and functionspackages
In packages, the name that starts with a capital letter is exported name, and when the import package is in, only exported name can be accessed externally.
Functions
A continuous parameter of the same type can be specified only at the end of the type.
A function can have more than one return value.
func swap(x, y string) (string, string) { return y, x}
Go supports the return value with Name:
- When a function is defined, it is defined to return the variable name, and the return variable is manipulated within the function.
- Returned with the naked return statement.
func split(sum int) (x, y int) { x = sum * 4 / 9 y = sum - x return}
Note: It is recommended to use this only in short functions, because it can easily affect readability.
Variables
The var keyword defines the variable.
You can omit the type when there is an initial value.
Tips and points of attention:
- Within a function, you can use the: = symbol to replace the variable definition with the initial value.
- But outside of the function, all statements must start with a keyword, so you cannot use the: = symbol.
Basic types
boolstringint int8 int16 int32 int64uint uint8 uint16 uint32 uint64 uintptrbyte // alias for uint8rune // alias for int32 // represents a Unicode code pointfloat32 float64complex64 complex128
Skills:
- Both Var and import can declare multiple packages or variables in parentheses.
- It is recommended that you do not need to specify a size or sign if you have no special needs.
variable is defined, if no initial value is specified, the default value of the corresponding type is assigned.
- Numeric type:0
- Bool:false
- String: ""
The expression T (V) indicates that the value V is converted to type T:
var i = 10var f = float64(i)
Note: Unlike the C language, go must be explicitly converted.
The constant definition replaces var with the Const keyword, but cannot use the: = symbol.
Questions
- Numeric constants is high-precision values.
A Tour of Go:basics 1