Fundamentals 11 min read

Master Go’s Core Data Types: From iota to Strings Explained

This article provides a comprehensive guide to Go’s fundamental data types—including iota, integers, floats, booleans, strings, and multi‑line strings—complete with explanations, code examples, and common string operations, helping readers deepen their understanding of Go’s core type system.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Go’s Core Data Types: From iota to Strings Explained

Introduction

In the previous article we covered Go variables; this article continues with Go’s basic data types.

iota

iota

is a constant counter that can only be used with the const keyword. It resets to 0 each time a const block appears and increments by one for each new constant line.

iota在const关键字出现时将被重置为0
const中每新增一行常量,iota将计数(+1)一次

Example 1:

package main

import "fmt"

func main() {
    const (
        n1 = iota // 0
        n2        // 1
        n3        // 2
        n4        // 3
    )
    fmt.Println(n1, n2, n3, n4)
}

Example 2 (using the blank identifier):

package main

import "fmt"

func main() {
    const (
        n1 = iota // 0
        n2        // 1
        _         // skip, iota becomes 2
        n4        // 3
    )
    fmt.Println(n1, n2, n4)
}

Example 3 (multiple values on one line):

package main

import "fmt"

func main() {
    const (
        a, b = iota + 1, iota + 2 // a=1, b=2
        c, d                      // c=2, d=3
        e, f                      // e=3, f=4
    )
    fmt.Println(a, b, c, d, e, f)
}

Integer Types

Go distinguishes signed and unsigned integers. Types that start with u cannot store negative values. uint8: unsigned 8‑bit integer (0 to 255) uint16: unsigned 16‑bit integer (0 to 65535) uint32: unsigned 32‑bit integer (0 to 4294967295) uint64: unsigned 64‑bit integer (0 to 18446744073709551615) int8: signed 8‑bit integer (‑128 to 127) int16: signed 16‑bit integer (‑32768 to 32767) int32: signed 32‑bit integer (‑2147483648 to 2147483647) int64: signed 64‑bit integer (‑9223372036854775808 to 9223372036854775807)

Platform‑dependent types: uint: uint32 on 32‑bit OS, uint64 on 64‑bit OS int: int32 on 32‑bit OS, int64 on 64‑bit OS uintptr: unsigned integer large enough to hold a pointer

Example:

package main

import "fmt"

func main() {
    var a = 10
    fmt.Printf("%T
", a) // int (default int64 on 64‑bit)
    fmt.Printf("%d
", a) // decimal
    fmt.Printf("%b
", a) // binary
    fmt.Printf("%o
", a) // octal

    var b = 0b1010011010 // binary literal
    fmt.Printf("%d
", b) // 666

    var c = 077 // octal literal
    fmt.Printf("%d
", c) // 63

    var d = 0x42 // hexadecimal literal
    fmt.Printf("%d
", d) // 66
}

Floating‑Point Types

Go provides float32 and float64; the default is float64.

package main

import "fmt"

func main() {
    var a = 1.21 // default float64
    fmt.Printf("%T
", a) // float64
    fmt.Printf("%f
", a) // 1.210000
    fmt.Printf("%.1f
", a) // 1.2
    fmt.Printf("%.2f
", a) // 1.21
}

Boolean Type

The bool type has only two values: true and false. Its zero value is false, and booleans cannot be mixed with integers.

String Type

Strings are a basic data type stored on the heap. They are defined with double quotes.

package main

import "fmt"

func main() {
    var name = "hello"
    var name2 = "张三"
    fmt.Println(name, name2)
}

Memory layout illustration:

String memory layout
String memory layout

Multi‑Line Strings

Back‑ticks ` allow raw multi‑line string literals.

package main

import "fmt"

func main() {
    var lyric = `
        昨夜同门云集bai 推杯又换盏
        今朝du茶凉酒寒 豪言成笑谈
        半生累 尽徒然zhi 碑文完美有谁看dao
        隐居山水之间 誓与浮名散
        湖畔青石板上 一把油纸伞
    `
    fmt.Println(lyric)
}

Common String Operations

len(str)

: length of the string in bytes + or fmt.Sprintf: concatenate strings strings.Split: split a string strings.Contains: check substring presence strings.HasPrefix / strings.HasSuffix: prefix or suffix check strings.Index / strings.LastIndex: position of a substring strings.Join: join a slice of strings with a separator

Example code (commented):

package main

func main() {
    // len() – string length
    // var name = "你好,世界"
    // fmt.Println(len(name))

    // Concatenation
    // name1 := "张"
    // name2 := "三"
    // name := name1 + name2
    // fmt.Println(name)

    // strings.Split
    // name := "张三|18|男|法外狂徒"
    // info := strings.Split(name, "|")
    // fmt.Println(info)

    // strings.Contains
    // name := "张三666"
    // result := strings.Contains(name, "6")
    // fmt.Println(result)

    // strings.HasPrefix
    // name := "web/student/xxx.html"
    // result := strings.HasPrefix(name, "web")
    // fmt.Println(result)

    // strings.HasSuffix
    // result := strings.HasSuffix(name, "html")
    // fmt.Println(result)

    // strings.Index
    // index := strings.Index(name, "o")
    // fmt.Println(index)

    // strings.LastIndex
    // index := strings.LastIndex(name, "o")
    // fmt.Println(index)

    // strings.Join
    // infoList := []string{"张三", "男", "18"}
    // name := strings.Join(infoList, "|")
    // fmt.Println(name)
}

Conclusion

We have covered Go’s fundamental data types. Feel free to leave questions in the discussion area.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GoData TypesStringsiota
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.