乐闻世界logo
搜索文章和话题

What are the different types of data types in Go?

2月7日 13:45

Data types in Go can be categorized into the following groups:

  1. Basic Types:

    • Boolean type: bool (values are true or false)
    • Numeric types:
      • Integer types: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr
      • Floating-point types: float32, float64
      • Complex types: complex64, complex128
    • String type: string
  2. Composite Types:

    • Array type: Declares a sequence of elements with a fixed size and type, e.g., var a [5]int
    • Slice type: A dynamic-size version of arrays where the length is not specified at declaration, e.g., var b []int
    • Struct type: struct, which organizes different data types into a composite entity, e.g.,
      go
      type Person struct { Name string Age int }
    • Pointer type: Used to store the memory address of a variable, e.g., var p *int
  3. Special Types:

    • Interface type: interface, which defines a set of method signatures but does not implement them; other types implement these methods, e.g.,
      go
      type Shape interface { Area() float64 Perimeter() float64 }
    • Map type: map, which stores key-value pairs where keys and values can be of different types, e.g., var m map[string]int
  4. Channel type (chan): Used for communication between multiple Go goroutines, e.g., chan int

Various data types support Go's powerful concurrency model and memory safety features, making it well-suited for systems programming and high-performance application development.

标签:Golang