Basic types of
bool
string
Signed integer
Int8, int16, int32, int64
Unsigned integer
Uint8, UINT16, uint32, uint64
Byte = Uint8 alias
An alias for rune =int32, representing a Unicode code point
Platform correlation type
type | 32 bit system | A 64 – bit system |
---|---|---|
int | int32 | int64 |
uint | uint32 | uint64 |
uintptr | uint32 | uint64 |
Floating point type
float32
float64
The compound type
Complex64, complex128
The structure of the body
Type [name] struct{}
Custom type
type CusInt int
Pointer types
*type
An array of
[n]T, arrays are fixed size in go
slice
[]T, slice and array are almost the same, the only difference is that the slice size is not fixed, arrays can be easily converted into slices, not vice versa
Methods and interfaces
function
// The following code in any go file defines a method
func Method(a){}// The go support method has multiple return values, and you can use _ to ignore one of them when you don't care about it
func Method(a) (string,error){
return "".nil
}
// Ignore the return value
_,_:=Method()
Copy the code
Variable parameter method
/ /... Type means that the method receives an array of type, and the size of the array is variable, depending on the number of arguments passed by the caller
func Method(args ...type){
print(len(args))
}
Copy the code
interface
Interface types are commonly used for behavior constraints, and go is no exception. However, one useful aspect of an interface in GO is that it can be used as any type (an empty interface), as follows:
// Use the interface keyword directly to define any type of variable
var i interface{} =1
var s interface{} ="string"(i)
// The I,s variables cannot be used directly
print(i)
print(s)
// The correct usage is as follows:
i:=i.(int) // This is called an assertion in go, and will crash if I is not an int (except in switch statements).
// In general, it is safer to use the following method, with the ok argument to indicate whether the assertion was successful
i,ok:=i.(int)
if(ok){
print(i)
}
Copy the code
The variable declared by interface{} in GO returns the address of the variable, that is, the type of the pointer. To obtain the specific value to which the pointer points, I.(type), that is, the way of assertion, is required.
Declare an interface type
In Go, the compiler does not force subclasses to implement the interface, which means that the implementation is done implicitly
// We define a People interface type and specify that the interface has a constraint on the Name method
type People interface{
Name()
}
Copy the code
Implementing an interface
type ManPeople struct{}
func (people ManPeople) Name(a){}
// If ManPeople does not implement the Name method of People, it will not pass the pre-compile code check
var people People=ManPeople{}
Copy the code
Variable parameters of arbitrary types through interface implementation methods
// The Method Method will accept any number of arguments of any type
func Method(args ...interface{}){}// This Method only takes any number of arguments of type People
func Method(args ... People){}Copy the code
object-oriented
There is no concept of a class in GO, usually our code starts with a struct, and it is easy to emulate a class in Go
// First we define a structure that describes how data is stored in memory
type People struct{
Name string
}
// We can create an instance by dropping it
people:=People{}
people.Name="Name"
Copy the code