Go 또는 Golang의 기본 유형은 무엇입니까?
Go 또는 Golang에는 총
19
개의 기본 유형이 있습니다. 그것들은 아래에 언급되어 있습니다:bool
string
int
int8
int16
int32
int64
uint
uint8
uint16
uint32
uint64
uintptr
byte
rune
float32
float64
complex64
complex128
부울 유형
The bool
type is used for holding truthy/falsy values such as true
or false
.
var isAdmin bool // isAdmin variable has a type of `bool`
isAdmin = true
문자열 유형
The string
type is used to hold a sequence of characters. The characters are enclosed in double quotation marks symbol ( ""
).
var name string // name variable has a type of `string`
name = "John Doe"
int , int8 , int16 , int32 및 int64 유형
The int
types are used to hold numerical values including the negative and non-negative numbers.
The numbers after the word int
refer to the size. For example, the int8
type can hold values of 8-bit
size.
Each of the int
types can have negative and non-negative numbers up to a certain range. These are mentioned below:
- The
int8
type can have numbers ranging from-128
to127
. - The
int16
type can have numbers ranging from-32768
to32767
. - The
int32
type can have numbers ranging from-2147483648
to2147483647
. - The
int64
type can have numbers ranging from-2^63
(-2 to the power of 63) to2^63 - 1
(2 to the power of 63 minus 1). - The
int
type is a platform-dependent type which means that on a32
bit system it will be the same as theint32
type and on a64
bit system it will be the same as theint64
type.
var num int8 // num variable has a type of `int8`
num = -128
uint , uint8 , uint16 , uint32 및 uint64 유형
The uint
types are used to hold numerical values for non-negative numbers. It is also called as unsigned integer type.
The numbers after the word int
refer to the size. For example, the uint8
type can hold values of 8-bit
size.
Each of the uint
types can have non-negative numbers up to a certain range. These are mentioned below:
- The
uint8
type can have numbers ranging from0
to255
. - The
uint16
type can have numbers ranging from0
to65535
. - The
uint32
type can have numbers ranging from0
to4294967295
. - The
uint64
type can have numbers ranging from0
to2^64 - 1
(2 to the power of 64 minus 1). - The
uint
type is a platform-dependent type which means that on a32
bit system it will be the same as theuint32
type and on a64
bit system it will be the same as theuint64
type.
var num uint8 // num variable has a type of `uint8`
num = 255
uintptr 유형
You may never have to use the uintptr
type unless you are developing code related to Go runtime libraries. It is used to bypass the Go's type system and can be used as the reference of memory locations of C
or other system-level programming languages codes.
바이트 유형
The byte
is an alias for the uint8
type.
룬 종류
The rune
is an alias for the int32
type.
One cool thing with the rune
type is that if you assign a Unicode symbol like 😃
(Smiling Face with Open Mouth) to the variable, then it will be automatically converted into its hexadecimal Unicode codepoint. The hexadecimal is essentially an integer.
var favEmoji rune
favEmoji = '😃'
fmt.Println(favEmoji) // 128515
float32 및 float64 유형
The float32
and float64
type is used to hold numerical values having a decimal or fractional part.
- The
float32
type has a32
bit size and has single precision. - The
float64
type has a64
bit size and has double precision.
floatNum := 78.65 // floatNum variable has the type of float64
complex64 및 complex128 유형
Go can easily handle complex numbers also with the complex64
and complex128
types.
- The
complex64
type has a real part composed of thefloat32
type and an imaginary part composed of thefloat32
type. - The
complex128
type has a real part composed of thefloat64
type and an imaginary part composed of thefloat64
type.
It can be done like this,
complexNum := 5 + 8i // complexNum variable type is complex128
Or you can use the built-in complex()
function to create the real and imaginary parts of a complex number like this,
complexNum := complex(5, 8) // complexNum variable type is complex128
fmt.println(complexNum) // 5 + 8i
These are the available basic types in Golang.
That's all 😃!
이 정보가 유용하다고 생각되면 자유롭게 공유하세요 😃.
Reference
이 문제에 관하여(Go 또는 Golang의 기본 유형은 무엇입니까?), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/melvin2016/what-are-the-basic-types-in-go-or-golang-35eg텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)