Follow the code to learn Golang language. Today explains the basic types of Golang languages, and describes Boolean types and numeric types.
Learning Golang Language (1): Hello World
Learning Golang Language (2): Variables
Learning Golang Language (3): Type--Boolean and numeric types
Learning Golang Language (4): Type--string
Learning Golang Language (5): Type--array
Boolean type
The Boolean type is bool. The Go language provides built-in Boolean values of True and flase. The go language supports standard logic and comparison operations. The result of these operations is a Boolean value.
Boolean values and expressions can be used in the IF statement, in the condition of the For statement, and in the case of the switch statement.
Logical operators:
!: logical non-operator;
|| : Logical OR operator;
&&: Logic and operators
The comparison operator.
<,>, ==,!=, <=, >=
Numeric type
----shaping and floating-point
The Go language provides a number of built-in numeric types. Well-known types such as int, this type determines the appropriate length based on your system. On a 32-bit system is 32 bits and 64 bits on 64-bit systems. So in the go language, int and int32 are different types. If you want to be clear about its length, you can use Int32 or int64 and so on.
Complete integer types (symbols and unsigned) are Int8,int16,int32,int64 and Byte,uint8,uint16,uint32,uint64. Where Byte is the alias of the Uint8.
The values for floating-point types are float32 and float64 (no float type). 64-bit integers and floating-point numbers are always 64-bit, even on 32-bit architectures.
The default assignment for shaping variables is 0, and the default assignment for floating-point variables is 0.0
It is important to note that these types are all independent, and mixing these types to assign values to variables can cause compiler errors. For example:
package mainfunc main(){ var a int //通用整数类型 var b int32 // 32位整数类型 a = 15 b =a + a // 混合这些类型是非法的,这会导致编译异常 b = b + 5 // 5是常量(未定义类型),所以没有问题}
Type conversions are required if numeric operations or comparison operations are performed between different numeric types. The type is typically converted to the largest type to prevent loss of precision. Type conversions take the form of type (value). When converting a type to a small type, we can create the appropriate function to prevent the loss of precision. Example: Convert int to Uint8
func Uint8FromInt(n int) (uint8, error) { if 0 <= n && n <= math.MaxUint8 {// conversion is safe return uint8(n), nil } return 0, fmt.Errorf(“%d is out of the uint8 range”, n)}
----plural types
Go native support plural. There are two types of complex numbers: complex64 (the real part is a float32) and complex128 (the real part of the imaginary part is a float64). The form of the plural is: Re+im I. where re is the real part, IM is imaginary part.
The complex number can be created using the built-in complex () function or the represented containing the imaginary part value. The various parts of the complex number can be obtained using the built-in functions real () and the Imag () function.
For example:
var c1 complex64 = 5 + 10i fmt.Printf(“The value is: %v”, c1) // 这里会输出: 5 + 10i c := complex(50,100); fmt.Printf(“The value is: %v”, c1) //这里会输出: 50 + 100i
The complex number supports all arithmetic operators. The only comparison operation symbol that can be used for complex numbers is = = and! =.
The standard library has a complex package MATH/CMPLX that provides a variety of common operation functions for complex numbers.
If you do not need to consider memory issues, use the complex128 type as much as possible, because all functions in the standard library use the complex128 type.
---------------------------------------------
Welcome to pay attention to code surgery! Learn Golang together.