Variable declaration
Define the variable type specified when the variable formula
Define the variable type inference rely upon
go static, strongly typed languages (java, c ++, too).
Static language: a variable can be determined at compile-time type of language
dynamic languages: determine the variable type of language runtime
strongly typed languages: Once you determine the type, run-time can not be changed, even if the cast does not alter the original variable type
is weak type: the context is able to run the implicit data type conversionVariable names brief statement left with at least one new, global variables can not be defined
Variable name on the left there is at least one new
You can not define global variables
Multi-variable declaration
var a,b,c int = 1,2,3 var a,b,c = 1,2,3 var( name = "" age = 23 sex = "" )
Zero value
- Empty Analyzing a == nil
- int 0 float 0.0 string ""
- Slices are reference types, a zero value is nil
Variables must be defined, otherwise an error
Constant declaration
Defined way
Explicit typing:
const B string = "hello"
Implicit type definition: type inference using
const B = "hello"
Multi-constant declaration
const( P1 = "hello" P2 = "golang" )
If no initial value is a constant, consistent with the default constant value on line
Enumerated type
Use as set constants enumerated type, a set of data of the correlation value.
const ( SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4 )
Precautions
- Constant data type can only be: boolean, numeric (integer, floating point, complex numbers), character
- Constant never used, no error
- When explicitly specify the data type, you must ensure constant values consistent with the type, the need to do an explicit conversion.