This is a creation in Article, where the information may have evolved or changed.
Order
has been concerned about the development of Golang, the recent go 1.7 also released, so determined to learn Golang. This article is an excerpt from the Go learning note, while deepening the understanding of Golang.
Variable
Golang is a statically typed language, so the type of the change cannot be changed at run time.
A variable is defined with the var keyword, automatically initialized to a value of 0 (such as a string of 0 value is an empty string, and a bool 0 value of False,int 0 value is 0). If you provide an initialization value, you can omit the type of the variable, which is automatically inferred by the compiler.
var x intvar f float32 = 1.6var s = "abc"
Inside the function, you can define the variable in a more abbreviated ": =" Way
func main(){ x := 123}
You can define multiple variables at once.
var x, y, x intvar s, n = "abc", 123var ( a int b float)func main() { n, s := 0x1234, "Hello, World!" println(x, s, n)}
When assigning multiple variables, all related values are evaluated first and then assigned from left to right.
data, i := [3]int{0, 1, 2}, 0i, data[i] = 2, 100 // (i = 0) -> (i = 2), (data[0] = 100)
Special write-only variable "_", used for ignoring value placeholders.
func test() (int, string) { return 1, "abc"}func main() { _, s := test() println(s)}
The compiler uses unused local variables as errors.
Note the difference between the re-assignment and the definition of the new variable with the same name.
s := "abc"println(&s)s, y := "hello", 20 // 重新赋值: 与前 s 在同一一层次的代码块中,且有新的变量被定义。println(&s, y) // 通常函数多返回值 err 会被重复使用用。{ s, z := 1000, 30 // 定义新同名变量: 不在同一一层次代码块。 println(&s, z)}
Constant
The constant value must be a number, a string, and a Boolean value that can be determined at compile time. In a constant group, if the type and initialization values are not provided, it is treated the same as the previous constant. The constant value can also be Len, cap, unsafe. The function return value of the result can be determined by the compile period such as Sizeof.
const x, y int = 1, 2 // 多常量初始化const s = "Hello, World!" // 类型推断const ( // 常量组 a, b = 10, 100 c bool = false)func main() { const x = "xxx"// 未使用局部常量不会引发编译错误。}const ( s = "abc" x // x = "abc")const ( a = "abc" b = len(a) c = unsafe.Sizeof(b))
Enumeration
The IOTA keyword defines the self-increment enumeration value in a constant group that starts at 0 by the row count.
const ( Sunday = iota // 0 Monday // 1,通常省略后续行行表达式。 Tuesday // 2 Wednesday // 3 Thursday // 4 Friday // 5 Saturday // 6)
Reference type
Reference types include slice, map, and channel. They have a complex internal structure that requires the initialization of related properties in addition to memory requests.
a := []int{0, 0, 0} // 提供初始化表达式。a[1] = 10
Type conversions
Implicit type conversions are not supported, even from narrow-width conversions. Use parentheses to avoid priority errors. You cannot use other types when bool values are used.
var b byte = 100// var n int = b // Error: cannot use b (type byte) as type int in assignmentvar n int = int(b) // 显式转换*Point(p) // 相当于 *(Point(p))(*Point)(p)<-chan int(c) // 相当于 <-(chan int(c))(<-chan int)(c)
String
A string is an immutable value type that is internally used to point to a UTF-8 byte array. Supports cross-line using "'" to define an original string that does not escape processing. When you concatenate a string across a line, "+" must be at the end of the previous line or cause a compilation error. To modify a string, you can first convert it to []rune or []byte, and then convert it to string. Regardless of the conversion, the memory is redistributed and the byte array is copied.
• The default value is an empty string "".
• Use an index number to access a byte, such as S[i].
• Cannot use ordinal to get byte element pointer, &s[i] is not illegal.
• Immutable type, no byte array can be modified.
• The tail of the byte array does not contain NULL.
Pointer
Supports pointer type t, pointer *T, and <package> containing the prefix of the package name ;. T.
• Default value nil, no NULL constant.
• The operator "&" takes the variable address, "" toaccess the target object through the pointer.
• Pointer operations are not supported, the "-and" operator is not supported, and direct use of "." is used to access the target member.
func main() { type data struct{ a int } var d = data{1234} var p *data p = &d fmt.Printf("%p, %v\n", p, p.a) // 直接用用指针访问⺫目目标对象成员,无无须转换。}//输出:0x2101ef018, 1234
Custom Types
You can divide a type into named and unnamed two large classes. Named types include bool, int, string, and so on, and arrays, slice, maps, and so on, are related to specific element types, lengths, and so on, which are unnamed types.
Unnamed types with the same declaration are considered to be of the same one by one type.
• Pointers with the same base type.
• An array with the same element type and ⻓ length.
• Slice with the same element type.
• Map with the same key-value type.
• Channel with the same element type and the direction of the sender.
• Anonymous struct with the same field sequence (field name, type, label, order).
• Function with the same signature (parameters and return values, excluding parameter names).
• The Square method set is the same (party method name, party method signature is the same, and the order has nothing to do with interface).
You can use the type to define a new type within a global or function. The new type is not an alias of the original type, except that they have the same data storage structure, there is no relationship between them, and no information of the original type is held. Unless the target type is an unnamed type, you must explicitly convert it.
x := 1234var b bigint = bigint(x) // 必须显式转换,除非非是常量。var b2 int64 = int64(b)var s myslice = []int{1, 2, 3} // 未命名类型,隐式转换。var s2 []int = s
package mainimport "FMT" type MyInt intfunc (i MyInt) Set (n MyInt) {i = N}func main () {var i MyInt = 2 I.set (FMT). Println (i)}//named type is passed by value, to change the value takes the pointer type to handle the func (i *myint) Set (n MyInt) {*i = n}