← Back to articles

Learning Go (4): basic types

Part 4 of my Go fundamentals notes: syntax, examples, and lessons learned.

Basic data types

Go’s basic types include bool, string, int, floating-point types, byte, rune, uint, int8, int16, int32, int64, uint8, uint16, uint32, uint64, float32, and float64.

They can be grouped as follows:

go

1. Numbers, floating-point values, and characters

1.1. Booleans

true and false

1.2. Numeric types

1.2.1. Integers

Different widths let us account for storage requirements.

int8

int16

int32

int64

uint8

uint16

uint32

uint64

int

uint

The width of int and uint depends on the target architecture: normally 32 bits on a 32-bit target and 64 bits on a 64-bit target.

Other integer types are shown here: go

1.2.2. Floating-point types

float32

float64

These represent 32-bit and 64-bit floating-point numbers.

1.2.3. Complex numbers

Go provides complex64 and complex128 for complex numbers.

The standard library also has a package for complex arithmetic.

import "math/cmplx"

1.2.4. Bytes

Go has no char type. byte is an alias for uint8, representing an 8-bit byte, commonly used for binary data and byte-oriented text processing.

var b byte = 'A'

1.2.5. Runes

rune is an alias for int32 and represents a Unicode code point, commonly used when processing Unicode text.

var r rune = '中'

1.3. Characters and strings

A single byte-sized character can be stored as byte.

Strings use string.

var name string
name = "aorange"

2. Converting basic types

The byte alias makes the intended byte-oriented use clearer while retaining the same underlying type as uint8.

var a int8 = 12
var b = uint8(a)

var f float32 = 12.3
var g = int(f)

// 字符转换为数字,这种情况可能会出错,有err返回
var isrt = "12"
var i, err = strconv.Atoi(isrt)
if err!= nil {
    fmt.Println("转换失败")
}

//数字转字符串类型,理论上不会出错,没有err返回
var i = 12
var s = strconv.Itoa(i)

3. Formatting and parsing conversions

Convert strings to float32 or bool. Convert other values to strings.

var s = "12.3"
var f, err = strconv.ParseFloat(s, 32) // 32表示32位浮点数

//ParseInt进行进制转换
parseInt , err := strconv.ParseInt(s: "12", 8, 64)//8表示8进制,64表示64位
//此时输出结果为10进制的10

//有关bool类型的转换
parseBool, err := strconv.ParseBool(s: "true")
//转换失败parseBool结果为零值,即false,err不为空
//true,t,T,y,Y,1,
//false,f,F,n,N,0
//其他结果转换可能失败

//基本类型转字符串
strconv.FmoatBool(b: true)
fmt.Println(b)
//其他的转换可以找源码看看,都差不多,都是用的strconv.FormatXXX

Here is a conversion failure example: go

4. Operators and expressions

I look up the details when needed; many common operators resemble those in C++.

4.1. Arithmetic operators

go

4.2. Comparison operators

go

4.3. Logical operators

go

4.4. Assignment operators

go

4.5. Bitwise operators

Bitwise operations can help in appropriate performance-sensitive algorithms, but they must preserve the required meaning; measure rather than assuming they always beat arithmetic.

go

go

go

4.6. Operator precedence in Go

The precedence levels are shown here: go

go

These basics become familiar through use. Data types and conversions have many similarities across languages.

About this article’s date

The date recorded on the old site is retained; writing may have started earlier.

In this series · Go fundamentals

PreviousLearning Go (3): variables and constants ←NextLearning Go (5): strings →