This is a creation in Article, where the information may have evolved or changed.
Go basic data type
1.bool,一个字节,值是true或者false,不可以用0或者1表示(java中boolean占用4个字节,而boolean作为数组出现时,每个boolean占用1个字节)2.int/uint(带符号为与不带符号位的int类型):根据平台不同是32位或者64位3.intx/uintx:x代表任意位数,例如:int3,代表占3bit的int类型4.byte占用8位,一个字节,相当于uint8,不带符号位5.floatx:由于没有double类型,所以float64就是double。float32小数精确到7位,float64小数精确到15位。6.complex64/complex128:复数类型7.uintptr:保存指针用的类型,也是随着平台改变而改变,因为指针的长度就是随平台而变。8.其他类型值:array,struct,string9.引用类型:slice,map,chan10.接口类型:interface11.函数类型:func
Type 0 Value
零值不等于空值,是说当变量被声明为某种类型后的默认值。一般情况下,值类型默认为0,bool默认为false,string为空串。
Variable Declaration and assignment
示例:package mainimport ("fmt""../wang")var ( aa = 1 bb = 2 cc = 3)const ( a1 = 1 a2 = 2 a3 = 3) type ( w1 int w2 int w3 string)func main() { var aaa w3 = "haha" var bbb w3 bbb = "wawawa" wang.Wang1() fmt.Println("Hello World!" + bbb + "" + aaa)}
- Import,var,const,type, when defining multiple, can be abbreviated with parentheses. You can also use parallel methods, and local variables can only be used in parallel.
- The package name inside the import is the starting point relative to the path to this go file. The above: /wang is the proof that it represents the physical path, not the package that is written on each file. The pkg name is a prefix for the contents of the reference package, for example: Wang. Wang1 ().
- A const declaration constant must be assigned a value.
- The type represents the name of the definition, just like in the database. This defines W1,W2,W3, respectively, to make them represent int,int,string. These are the three original types of aliases, then you will write these aliases later. and cannot omit writing.
- Local variables cannot be defined in the form of a global variable, which is used in a parallel manner.
- When defining a variable declaration: var a int =10;var a=10;a:=10 ——-> The first is the standard notation, and the second, three means defining and assigning values, and the compiler determines which type is based on the assignment. The third kind of shorthand cannot define global variables, and the compiler will make an error.
- Go language private variables, constants, functions, etc., the beginning of the letter is lowercase, the public the beginning of uppercase letters.
- Only the main package has the main function, and the main function cannot have a return value, main lowercase.
- Defined local variables, the introduction of the package must be used, if not, compile error.
Parallel mode: var a,b,c,d [int] = 1,2,3,4;
Special: Var A,c,d = 1,2,3,4--> for whitespace, equivalent to occupy position, so the value of 2 is bye. The actual meaning is reflected in the function.