This is a creation in Article, where the information may have evolved or changed.
Plastic
Example of type conversion
var ia int64 = 54345var ib Int32ib = Int32 (ia) fmt. Println ("Ibis", IB)
Float type
The Go language defines two floating-point characters float32 is equivalent to the C-language float type,
Float64 double type equivalent to the C language Comparison of floating-point numbers math. Fdim (F1, F2) < p p for defined accuracy such as 0.00000001
String
var str1 stringstr1 = "This was first string" str2: = "This is second string" STR3: = str1 + str2fmt. Printf ("The string str3:%s \ n len:%d \ n First char is%c \ n", Str3, Len (STR3), str3[0])
Strings can be traversed using the usual method for looping, using Len's string length, using str3[i] to take a value, according to the subscript string character, type Byte, here is another way to traverse:
For i, ch: = Range Str3 {//fmt. When Println (i, ch, "\ n") are traversed in Unicode characters, the type of each character is runefmt. Printf ("%d,%c", I, CH)}
Character type
Go supports two types of characters: byte (actually an alias of Uint8), representing the value of a single byte of the UTF-8 string
Rune represents a single Unicode character for rune related operations and can be consulted on the Unicode package of the Go standard library. In addition, the Unicode/utf8 package also provides conversion between UTF8 and Unicode.
Note : For simplified language considerations, most APIs in the go language assume that the string is UTF-8 encoded. Although Unicode characters are supported in the standard library, they are actually less used.
Array
The array in Go is made up of a set of elements of the same type that cannot be changed after its length is declared, and the array general declaration method
[32]byte//array of length 32, each element is a byte [2*n] struct {x, y Int32}//Complex type array [1000]*float64//pointer array [3][5]int//two-D array [2][2][2]float64 equivalent to [2] ([2] ([2]float64))
Go in array small with C, starting from 0 Array traversal
var array [2]struct {x stringy uint64}array[0].x = "First" ARRAY[0].Y = 888888array[1].x = "Secon" array[1].y = 99999for I, V: = range array {fmt. Println ("Array element[", I, "]=", v.x, V.y, "\ n")}
Note:In go, arrays are value types, and all value type variables are assigned and passed as arguments.
generates a copy action.
Array slices
At first glance, an array slice is like a pointer to an array, and in fact it has its own data structure, not just a pointer. The data structure of an array slice can be abstracted into the following 3 variables:
- A pointer to the native array;
- The number of elements in the array slice;
- The array slice has allocated storage space.
There are two ways to create an array slice
based on array or array slices You can create an array slice that is less than or equal to, or even larger than the original array, based on an existing array go language support with Myarray[first:last]To create an array slice: myarray[: ]Whole myarray[: 5 ]The first five, myarray[5: ]Start with the fifth one.
First define an array var myArray [10]int = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}//create an array slice based on an array var myslice []int = myarray[:5]fmt. Println ("Elements of MyArray:") for _, V: = Range MyArray {fmt. Print (V, "")}fmt. Println ("\nelements of Myslice:") for _, V: = Range Myslice {fmt. Print (V, "")}fmt. Println ()
directly Create
Go can use built-in function make (), flexibility to create array slices makes the first parameter defines the array type such as []int, the second parameter is the number of elements, the third parameter is reserved storage space, you can default
Create an array slice with an initial element number of 5, with an element initial value of 0:myslice1: = Make ([]int, 5) creates an array slice with an initial element number of 5, an element with an initial value of 0, and a storage space of 10 elements: MySlice2: = make ([]int, 5, 10) directly creates and initializes an array slice with 5 elements: mySlice3: = []int{1, 2, 3, 4, 5}
Elements traverse the same array
dynamic Increase or decrease Append () can be used in go to add elements to the tail of an array slice, the second parameter of the function is an indefinite length parameter, and may be descendants on demand You can also slice an array with ... After breaking up, add to the array slice after
ARR1: = Make ([]int64, 5) for _, V: = Range arr1 {fmt. Print ("", V)}fmt. Println () arr1 = Append (arr1, 3, 4, 5) for _, V: = Range arr1 {fmt. Print ("", V)}fmt. Println () arr2: = []int64{8, 8, 8, 8, 8}arr1 = Append (arr1, arr2 ...) For _, V: = Range arr1 {fmt. Print ("", V)}fmt. Println ()
Content Replication
Slice1: = []int{1, 2, 3, 4, 5}slice2: = []int{5, 4, 3}copy (Slice2, Slice1)//Only the first 3 elements of Slice1 are copied to Slice2 in copy (Slice1, Slice2) Only 3 elements of Slice2 are copied to the first 3 positions of the Slice1
MAP
In go, map is built-in and is an unordered collection of key-value pairs.
<span style= "White-space:pre" ></span>type book struct {Name stringactor stringprice Float64}var Booksmap Map[string]book //Declaration Booksmap = Make (Map[string]book)//Create booksmap["123456"] = book{"Go Web Programming", "TOM.SD", 64.2}booksmap["443"] = book{"Go programming Language", "tom.ddd", 55.2}book1, OK: = booksmap["443"]//Find if OK {fmt. Println ("Book1", Book1. Name, Book1. Actor, Book1. Price) Book1. Price = 88.8booksmap["443"] = Book1 //Modify}BOOK2, OK: = booksmap["443"]if OK {fmt. Println ("Book1", Book2. Name, Book2. Actor, Book2. Price)//book1. Price = 88.8}
Delete Element
Delete (Booksmap, "443")