The logical value is represented by bool type. As shown below, the value is ** true ** if true and ** false ** if false. You can also define it with type inference.
var a bool //Define variable a as bool type
a = true //Assign true to variable a
a := true
Integer types are available as numeric types, and there are two types: ** signed integer type ** and ** unsigned integer type **.
Signed integer types include int8
, int16
, int32
, and int64
, and the numbers represent the size.
There are uint8 (byte) `` `,
uint16,
uint32,
uint64types of unsigned integer types, and the number is also the size. Represents. Unlike signed,
uint8 can also be defined with an alias of `` `byte
type, which behaves the same as uint8.
Also, in the 64-bit implementation of Go, the int
type behaves the same as the int64
, but as shown below, the variables that represent the same 64-bit integer are However, because the types are different, implicit type conversion will result in compilation errors.
a := int(10)
b := int64(5)
b = a //Compile error
fmt.Printf("%v\n", b)
In this way, if you try to perform type conversion implicitly, you will get a compile error. However, if you specify the type firmly as shown below and perform type conversion explicitly, you can execute it without causing a compile error.
a := int(10)
b := int64(a)
fmt.Printf("%v\n", b)
This method allows you to process numbers that exceed the maximum type size.
a := int8(128) //Compile error
fmt.Printf("%v\n", a)
a := 128
b := int8(a)
fmt.Printf("%v\n", b)
It is now possible to process at 128, which exceeds the maximum value of ** int8, 127 **. By doing this explicitly, ** overflow ** occurs on the binary representation when 1 is added to the maximum value 127, ** wraparound **, and the minimum value. Because it will return to. So, in this case, the output value will be -128 **, which is the minimum value of ** int8.
By importing and using the ** math package ** as an up-around measure, you can write code while being aware of the overflow of digits.
There are two types of floating point types: `float32: single precision floating point ``` and
`float64: double precision floating point ```. By the way, float32 is less accurate than float64, and if you don't consider memory usage efficiency, there seems to be little merit in using it unless you have a specific reason.
a := 1.0 //float64
b := float32(1.0) //float32
Floating-point types also have constants that represent a range of numbers in the math package, and you need to perform explicit type conversion if you want to use floating-point literals to take different types of values. However, the decimal point is truncated.
Next is the special value that makes the operation impossible. The output results of the following operations have the meanings of + Int (** positive infinity ), -Int ( negative infinity ), and NaN ( non-number **), respectively.
1.0 / 0.0 //+Int
-1.0 / 0.0 //-Int
0.0 / 0.0 //NaN
A is the real part, b is the imaginary part, and i is the imaginary unit, which is represented by the literal ** a + bi **, and has two types, complex64
and `complex128```. Is available.
Float32``` is used for the real part and imaginary part of complex64 type, respectively, and ``
float64``` is used for the real part and imaginary part of complex128 type.
The following is an assignment method using complex literals. By the way, the + sign is just a part of the literal, not an operator. You can also use the complex function to return and generate each value by taking the arguments of the real part and the imaginary part.
a := 1.0 + 2i
a := complex(1.0, 2)
There is also a function to retrieve the real and imaginary values of this literal.
a := 1.0 + 2i
real(a) //1.0
imag(a) //2
This is a type that represents a character, and the character enclosed in single quotation marks is represented by Unicode code points and output. You can also represent Unicode characters by adding a prefix that starts with a backslash. There are also escape sequences for string representations. It is defined as another name int32 and works the same.
a := 'Example'
fmt.Printf("%v", a) //20363
a := '\U00101234'
fmt.Printf("%v", a) //1053236
It is defined as a string type and provides a string literal enclosed in double quotes.
a := "example"
fmt.Printf("%v", a) //example
a := "\u65e 5 pieces\U00008a9e"
fmt.Printf("%v", a) //Japanese
Another form of string literal is ** RAW string literal **. It uses back quotes and has the property of supporting multiple lines of strings and not processing those characters.
a := '
example
example
example
'
fmt.Printf("%v", a) //example
example
example
This time we have summarized the basic types of Go! I think you could understand not only the surface of the basic type itself, but also the meaning and mechanism to some extent.
I will continue to output at this rate!
Recommended Posts