This is a creation in
Article, where the information may have evolved or changed.
5. Toad Notes Go language-basic type
Basic type
The basic types of Go are basic types
bool
String
int int8 int16 int32 int64
UINT uint8 uint16 UInt32 UInt64 uintptr
BYTE//Uint8 Alias
Rune//Int32 aliases
Represents a Unicode code
float32 float64
Complex64 complex128
As with the import statement, the definition of a variable is "packaged" in a block of syntax.
Package Main
Import (
"FMT"
"MATH/CMPLX"
)
VAR (
ToBe bool = False
MaxIntuint64 = 1<<64-1
Z complex128 = cmplx. SQRT ( -5 + 12i)
)
Func Main () {
Const f= "%T (%v) \ n"
Fmt. Printf (F,tobe, ToBe)
Fmt. Printf (F,maxint, MaxInt)
Fmt. Printf (F,z, z)
}
EXECUTE as follows:
BOOL (FALSE)
UInt64 (18446744073709551615)
complex128 ((2+3i))
0 value
A variable is assigned a value of _ 0 when it is not explicitly initialized at the time of definition.
The 0 values are:
The value type is ' 0 ',
The Boolean type is ' false ',
The string is ' "' (empty string).
Package Main
Import "FMT"
Func Main () {
var iint
var Ffloat64
var Bbool
var sstring
Fmt. Printf ("%v%v%v%q\n", I, F, B, s)
}
Perform:
0 0 false ""
Type conversions
The expression T (V) converts the value V to type ' t '.
Some conversions on numeric values:
var i int = 42
var f float64 = float64 (i)
var u uint = UINT (f)
Or, in a simpler form:
I: = 42
F: = Float64 (i)
U: = UINT (f)
Unlike C, it is necessary to explicitly convert a project assignment between different types of Go. Try removing the float64 or int conversions from the example to see what happens.
Package Main
Import (
"FMT"
"Math"
)
Func Main () {
var x, Y int = 3, 4
var ffloat64 = Math. Sqrt (float64 (x*x + y*y))
var zint = int (f)
Fmt. Println (x, Y, z)
}
Perform:
34 5
Type deduction
When you define a variable without specifying its type (using VAR with no type or: = statement), the type of the variable is inferred from the right value.
When the right value defines a type, the new variable is of the same type:
var i int
J: = I//j is also an int
But when the right side contains numeric constants of unnamed types, the new variables may be int, float64, or ' complex128 '. This depends on the precision of the constant:
I: = +//int
F: = 3.142//float64
G: = 0.867 + 0.5i//complex128
Try to modify the initial value of V in the demo code and see how this affects its type.
Package Main
Import "FMT"
Func Main () {
V: = 42//change me!
Fmt. Printf ("Vis of type%t\n", V)
}
Perform:
Vis of type int
Constant
The definition of a constant is similar to a variable, except that it uses the Const keyword.
Constants can be values of characters, strings, booleans, or numeric types.
Constants cannot be used: = Syntax definitions.
Package Main
Import "FMT"
Const PI = 3.14
Func Main () {
Constworld = " World "
Fmt. Println ("Hello", World)
Fmt. Println ("Happy", Pi, "Day")
Consttruth = True
Fmt. Println ("Gorules?", Truth)
}
Perform:
Hello World
Happy3.14 Day
Gorules? True
Numeric constants
A numeric constant is a high-precision _ value _.
A constant of an unspecified type is determined by the context to determine its type.
Also try out the output needint (Big).
Package Main
Import "FMT"
Const (
Big = 1 << 100
Small =big >> 99
)
Func needint (x int) int {return x*10 + 1}
Func needfloat (x float64) float64 {
RETURNX * 0.1
}
Func Main () {
Fmt. Println (Needint (Small))
Fmt. Println (Needfloat (Small))
Fmt. Println (Needfloat (Big))
}
Execution output:
21st
0.2
1.2676506002282295e+29