This is a creation in Article, where the information may have evolved or changed.
First knowledge of Go language
Go is a new language that provides concurrent support (Goroutine) and communication tool channel at the language level, as well as a compiled, strongly typed language with a memory GC mechanism. The module of the program is organized through the package.
Go Basics
Integer
Floating point number
Floating-point numbers consist of an integer part, a decimal point, and a fractional part, which can be hidden by integral and fractional parts. You can also use scientific notation to indicate:
- 0.
- 72.40
- Like 072.40 and 72.40.
- 2.71823
- 1.e+0
- 6.67428e-11
- 1E6
- .25
- .12345E+5
Imaginary part of a complex number
is represented by an integer or a decimal plus I
- 0i
- 011i//==11i part of the octal here.
- 0.i
- 2.71825i
- 1.e+0i
- 6.23423-11i
- 1e6i
- .25i
- .1234e+5i
Rune literals
In go, you can use a Rune (int32) type to represent Unicode characters
String
stringThere are two forms of representation
- Raw string, all characters are in the original form, do not transfer, the return output is a newline, cannot represent itself
this is string
second line
- A string of double quotes in the same way as C + + +, which means the same as basic and C + +
The go string supports Unicode encoding
Type
goCommand type and anonymous type are supported, and anonymous types are primarily for struct
Boolean, numeric, and string are language built-in types; combination type (array, struct, pointer, function, interface, slice, map, and channel Types) can be constructed using the syntax of type
Any type T has a base type, and if a type T is a built-in type or a composite type, its base type is itself, otherwise, the base type of T is the type of the point to which the type is declared
- Type T1 string base type is string
- Type T2 T3 base type is string
- Type T3 []T1 basic type is []T1
- Type T4 T3 basic type is []T1
Method
A type can have a collection of methods, and a collection of interface methods is his interface. A collection of methods for a type is declared by declaring a function and the function has a receiver of that type, as follows:
Func (T-t) funcname (param T1) {}
Func means that funcname is a function, (t) is a function whose receiver,funcname is the name of the function, and Param is the parameter of the function.
Pointer-type *t method collection contains the method set of T and Receiver-*t method
What functions in the go language are implemented by one type to determine which interfaces they implement, rather than deriving by direct inheritance.
Boolean type
A Boolean is represented by True and false, and its built-in type isbool
numeric types
Numeric types include integer and float, built-in and system-independent numeric types include
- Uint8 8-bit unsigned integer (0 to 255)
- UInt16 16-bit unsigned integer (0 to 65535)
- UInt32 32-bit unsigned integer (0 to 4294967295)
- UInt64 64-bit unsigned integer (0 to 18446744073709551615)
- Int8 8-bit signed integer ( -128 to 127)
- Int16 16-bit signed integer ( -32768 to 32767)
- Int32 32-bit signed integer ( -2147483648 to 2147483647)
- Int64 64-bit signed integer ( -9223372036854775808 to 9223372036854775807)
- Float32 32-bit floating-point type
- Float64 64-bit floating-point type
- complex32 by float32 Real part + imaginary part
- Complex64 by float64 Real part + imaginary part
- The alias of Byte uint8
- Rune Int32 's Alias
Platform-related types
- Uint,int 32 or 64-bit
- UIntPtr An unsigned integer that is sufficient to represent the pointer
Explicit type conversions are required for mixed operations of different types
String
stringis immutable, and once created, the contents of the string cannot be changed.
var a string = "This is a test"
A[1] = ' A ' illegal, cannot change string the value
&a[i] Illegal, cannot string take address to the element
A[i] accesses the first byte Len (i) is the number of bytes, and Unicode may occupy more than one byte
For , ch: = Range A {} and for, Uni: = range []rune (a) {} can traverse each Unicode of a string
For _, ch: = range []byte (a) {} can traverse every byte of a string
Array
An array is a set of fixed-length types, length is the len of the array, and the type is the element type of the array.
Index starting from 0, the array can be multidimensional
- [32]byte
- [2*n]struct {x, y int32} N must be a constant, where the struct is an anonymous struct
- [1000]*float64
- [2] [3]int
- [2] [2] [2]float64
Slice
Slice is used to describe a underlying array and provides the ability to access the elements of the array. Uninitialized slice is nil.
Because an array is a value type, it is a waste of space to pass a copy of the array when the function argument is passed, and the second is to not modify the array within the function, so you can use a slice with the same element type for parameter passing because slice points to an array, So you can modify the array by slice.
Slice created with make ([]t, length, capacity)
One of the third parameters can be omitted.
You can add a new element to the end of slice by append
var sli make([]byte, 0, 100append(sli, 10)
Append returns a new slice, and when the append returns a new slice longer than the SLI capacity, a new array is regenerated and the new slice points to the new array. That is, the new slice returned by append does not necessarily point to the original array.
var[3]byteappend(b, 1)d[1'b'//[0 0 0]//[0 98 0 1]
sliceLike array, it also supports multidimensional slice, such as
- []byte
- []int
[][]int
Slice how to create
var a = make ([]int, 3,3) is created by making
- var a = []int{} An empty slice
- var a = [3]int{}; B: = a[:] Created by an array, at this point the underlying array of B is a
struct
One struct consists field of each consisting of field a name and a type.
fieldCan be named and not named, non blank field must be unique
struct {} empty struct
struct{
x, y int
U float32
_ Float32//black Field
A *[]int
F func ()
}
Unnamed members, also known as nested members, will have a collection of methods for nested members, with the following rules:
- If s contains an anonymous member T, then S and *s both contain a method collection of T, and *s also contains a collection of *t methods
- If s contains anonymous member *t, both S and *s contain the T and *t method collections
struct {
T1
*t2
}
The type name of the unnamed member is named as the member, so the following declaration is incorrect
struct{
T1
*t1
}
This creates a naming conflict for the member
Members of the struct can also contain an optional tag
struct {
T1 "Any string"
}
Tag is a property of a struct member and can be viewed through reflect.
Pointer
Pointers can point to any type, declared by *+type
function
A function type table has functions that have the same types of parameter types and return values.
In the argument list of a function, the parameter name must either be all or none, and not only some of the arguments have no name.
The list of returned values is the same as the argument list, and when the return value list has a name, the names can be used in the function body as well as the parameter names.
Parameter support variable parameter list, using ... T indicates that the mutable parameter must be in the last face of the parameter list.
function can have multiple return values
- Func ()
- Func (x int) int
- Func (A, _ int, z float32) bool
- Func (a int, b int32) (ret bool)
- Func (a int) (bool, int)
- Func (A, b int, c ... int)
Interface
An interface type indicates a collection of methods for an interface, and any type that implements the collection of the method implements the interface and can be assigned to a variable of that interface type.
Type Readwriter Interface {
Read (b Buffer) bool
Write (b Buffer) bool
Close ()
}
The method name of the interface cannot be named, and any type can implement an interface, as long as the type has the following methods
Func (P T) Read (b Buffer) bool {}
Func (P T) Write (b Buffer) bool {}
Func (P T) Close () {}
A non-type can implement multiple interfaces, only need to have interfaces corresponding to the method, such as all types have implemented the following null interface.
interface{}
Interfaces also support nesting, but nested interfaces cannot have methods with the same name
go
type T1 interface {
Read(b Buffer) bool
}
type T2 interface {
T1
Write(b buffer) bool
}
Legal
go
type T2 interface {
T1
Read(b Buffer) bool
}
Not legal
Simultaneous interface nesting cannot be recursive and self-contained.
Map
The go map is an unordered collection of k-v, Map[key type]element type
Where key type must be enabled for = = and! =, so key type cannot make function, map, or slice. If key is an excuse type, the actual type that the interface points to must support = = and! =
- Map[string]int
- Map[*t]struct{x, y float64}
- map[string]interface{}
Make (map[key]element, capacity) can create a map
Channel
channelProvides a mechanism for goroutine peer, similar to a Linux pipe, but is first concurrency-safe.
-chan int bidirectional channel
-chan<-int can only send the channel
-<-chan int can receive only the channel
channelCan be buffered or not buffered, created with make
Make (chan int) creates a channel with no buffering
Make (chan int, 100) creates a channel with 100 buffers
When the channel is received, if the channel's buffer is empty, the current goroutine is blocked until new data is desirable
When sending, if the Chuannel buffer is full, block the current goroutine until the buffer is dissatisfied