GoLang - Data Types
In this tutorial, you will learn about data types in Go Programming with the help of examples.
Data types in Go programming language are used to define the type of value that a variable can hold. There are several built-in data types available in Go, which can be categorized into four categories - Basic, Composite, Reference, and Interface types.
Basic Data Types
The basic data types in Go include:
Boolean
It represents a true or false value. The type is bool and the value can be either true or false.
Example:
var isTrue bool = true
var isFalse bool = false
Numeric Types
Numeric types in Go include integers and floating-point numbers. Integers can be of different sizes, including 8-bit, 16-bit, 32-bit, and 64-bit integers. Floating-point numbers can be of two types, float32 and float64.
var i int = 10
var j int64 = 100000
var f float32 = 1.2345
var d float64 = 123.456789
String
A string is a sequence of characters enclosed in double quotes. The type is string.
Example:
var message string = "Hello, World!"
Composite Data Types
Composite data types in Go are used to represent a collection of values. They include:
Arrays
Arrays are a collection of elements of the same type, stored in a contiguous memory location. The size of the array is fixed at the time of declaration.
Example:
var numbers [5]int = [5]int{1, 2, 3, 4, 5}
Slices
Slices are similar to arrays, but their size is not fixed. They are a reference to an underlying array and can be resized dynamically.
Example:
var numbers []int = []int{1, 2, 3, 4, 5}
Maps
Maps are used to store key-value pairs. The type is map[keyType]valueType, where keyType and valueType are the types of the key and value respectively.
Example:
var details map[string]string = map[string]string{"name": "John", "age": "30"}
Reference Data Types
Reference data types in Go are used to represent a reference to a memory location. They include:
Pointers
A pointer is a variable that stores the memory address of another variable. The type of a pointer is *T, where T is the type of the variable being pointed to.
Example:
var x int = 10
var ptr *int = &x
Interface Types
Interface types in Go are used to define a set of methods that a type must implement. They include:
Interfaces
An interface is a collection of method signatures. A type implements an interface by implementing all the methods defined in the interface.
Example:
package main
import "fmt"
type Shape interface {
Area() float64
Perimeter() float64
}
type Rectangle struct {
width float64
height float64
}
func (r Rectangle) Area() float64 {
return r.width * r.height
}
func (r Rectangle) Perimeter() float64 {
return 2 * (r.width + r.height)
}
func main() {
var shape Shape = Rectangle{10, 20}
fmt.Println(shape.Area()) // Output: 200
fmt.Println(shape.Perimeter()) // Output: 60
}
In conclusion, understanding data types is crucial in Go programming, as it helps in writing efficient and correct code.
Frequently Asked Questions
Q: What is the default type for integers in Go?
The default type for integers in Go is int. On a 64-bit system, int is 64 bits wide, while on a 32-bit system it is 32 bits wide. If you need a specific size, you can use int8, int16, int32, or int64.
Q: What is the difference between float32 and float64?
float32 provides about 7 decimal digits of precision and uses 32 bits of memory. float64 provides about 15 decimal digits of precision and uses 64 bits. In most cases, float64 is preferred because it offers better precision with minimal additional memory cost.
Q: What is a rune in Go?
A rune in Go is an alias for int32 and represents a Unicode code point. It is used to handle individual characters, especially when working with multi-byte Unicode strings. You can declare a rune using single quotes: var ch rune = 'A'.
Q: Can I create custom data types in Go?
Yes. You can define custom data types using the type keyword. For example, type Age int creates a new type called Age based on int. You can also create complex types using structs, which group multiple fields together.