Let's talk about several common basic data types in Golang
青灯夜游
Release: 2022-06-30 11:34:43
forward
2701 people have browsed it
This article takes you through some commonly used basic data types in golang, such as integers, floating point types, characters, strings, Boolean types, etc., and introduces some commonly used types conversion operation.
Golang is a strongly typed language. When using variables, mandatory type definitions are required. Once a variable is defined, if it is not forced to convert, it will always be that type. type.
Variable declaration and initialization
Golang recommends using camel case naming, such as QuoteRuneToASCII and parseRequestLine.
Several forms of defining variables:
// 第一种,先声明类型,再进行初始化赋值
// 如果没有初始化,则变量默认为零值。
var a int
a = 3
// 第二种, 根据赋值自动适配类型
b := 3 // 这种写法只能在函数体中出现
// 效果等价于 var b = 3, 但后者可以在函数体之外的地方使用
// 第三种,声明类型的同时进行赋值
var c int = 3
// 第四种,使用 new 创建变量,然后返回变量的地址,类型为 *type
p := new(int)
*p = 3
// 与 python 相同,交换两个变量的值,可以使用 a, b = b, a
// 但与 python 不同的是两个变量的类型必须是相同的。
Copy after login
Note:
Unused variables are not allowed in Golang, otherwise an error will be reported a declared and not used. But global variables are allowed to be declared but not used.
The encapsulation of Golang is determined by the first letter of the variable name. Variables starting with a capital letter are exportable variables and can be used outside the package. Variables starting with a lowercase letter or an underscore can only be used within this package. .
":=" is a variable declaration statement, and "=' is a variable assignment operation. When multiple variables are briefly declared in one line, at least one new variable must be declared in the ":=" statement.
Basic data type
Get element type
// 方法一,创建 reflect 变量
import "reflect"
var a int
typeOfA := reflect.TypeOf(a)
fmt.Println(typeOfA)
// 或者直接 fmt.Println(reflect.TypeOf(a))
// 方法二,使用 fmt 查看
import "fmt"
var a int
fmt.Printf("%T", a) // 注意不能用 Println
Copy after login
Integer type
The number represents the bit length of the type. Types with different lengths are independent. Mixed calculations will report an error.
The length of int is determined by the hardware:
Data type
Computer architecture
Bit width
Byte width
int
32 bits
32
4
int
64-bit
64
8
uint
32-bit
32
4
##uint
64-bit
64
8
# #You can also specify the fixed-length type of int:
Data type
Bit width
int8
8
int16
16
int32 (rune, can be used Represents a Unicode character)
32
int64
64
uint8 (byte)
8
uint16
16
uint32
32
uint64
64
浮点类型
Golang 支持两种浮点型数值:float32 和 float64。可以使用 math 包中的常量获取浮点类型的边界值
// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8
// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32
The above is the detailed content of Let's talk about several common basic data types in Golang. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn