Go Data Types
Go Data Types
Data types in Go define the kind of values a variable can hold. Go is a language with static typing, which means a variable's type is determined during compilation.. This ensures type safety and better performance.
1. Basic Data Types
Go has several built-in data types categorized into the following groups:
1.1 Numeric Types
Integer Types
Go provides both signed and unsigned integers of various sizes:
- Signed Integers (int8, int16, int32, int64)
- Unsigned Integers (uint8, uint16, uint32, uint64)
- Machine-dependent Integer Sizes (int, uint, uintptr)
Example:
package main import "fmt" func main() { var a int = 42 var b uint = 100 var c int8 = -10 fmt.Println(a, b, c) }
Floating-Point Types
Go supports floating-point numbers using float32 and float64.
Example:
package main import "fmt" func main() { var x float32 = 3.14 var y float64 = 2.718 fmt.Println(x, y) }
Complex Numbers
Go includes built-in support for complex numbers using complex64 and complex128.
Example:
package main import "fmt" func main() { var c1 complex64 = complex(5, 7) var c2 complex128 = complex(2.3, 4.5) fmt.Println(c1, c2) }
1.2 Boolean Type
A boolean variable stores either true or false.
Example:
package main import "fmt" func main() { var isGoFun bool = true fmt.Println("Is Go fun?", isGoFun) }
1.3 String Type
In Go, strings are immutable and consist of a sequence of bytes.
Example:
package main import "fmt" func main() { var str string = "Hello, Go!" fmt.Println(str) }
2. Composite Data Types
2.1 Arrays
Arrays are fixed-size collections of elements of the same type.
Example:
package main import "fmt" func main() { var arr [5]int = [5]int{1, 2, 3, 4, 5} declares an array of five integers. fmt.Println(arr) }
2.2 Slices
A slice is a dynamically-sized, more flexible alternative to an array.
Example:
package main import "fmt" func main() { slice := []int{10, 20, 30, 40} fmt.Println(slice) }
2.3 Maps
A map is a data structure that stores values as key-value pairs.
Example:
package main import "fmt" func main() { person := map[string]int{"Alice": 25, "Bob": 30} fmt.Println(person) }
2.4 Structs
Structs allow defining custom data types by grouping related fields together.
Example:
package main import "fmt" type Person struct { Name string Age int } func main() { p := Person{"John", 28} fmt.Println(p) }
3. Interface Type
Interfaces define a set of method signatures that types can implement.
Example:
package main import "fmt" type Speaker interface { Speak() } type Human struct {} func (h Human) Speak() { fmt.Println("Hello, I am a human!") } func main() { var s Speaker = Human{} s.Speak() }
4. Pointer Type
Pointers store memory addresses of variables.
Example:
package main import "fmt" func main() { x := 42 p := &x fmt.Println("Value:", x) fmt.Println("Pointer Address:", p) fmt.Println("Value via Pointer:", *p) }
Conclusion
Go offers a rich set of data types, from primitive types like integers and floats to complex structures like maps and interfaces. Having a clear understanding of these types is essential for writing optimized Go programs.
PreviousNext