Hey guys, in this article you will learn about the basic variables and types in Golang.
First of all, what is Golang? Golang is a simple, objective and extremely efficient language created by Google Movements. The main objective is to do a lot with little code.
1. Basic Types
one. Integers
Signed integers:
int (size depends on architecture: 32 bits on 32-bit systems, 64 bits on 64-bit systems)
int8 (8 bits, -128 to 127)
int16 (16 bits, -32,768 to 32,767)
int32 (32 bits, -2,147,483,648 to 2,147,483,647)
int64 (64 bits, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
Unsigned integers:
uint (size depends on architecture: 32 or 64 bits)
uint8 (8 bits, 0 to 255; equivalent to byte)
uint16 (16 bits, 0 to 65,535)
uint32 (32 bits, 0 to 4,294,967,295)
uint64 (64 bits, 0 to 18,446,744,073,709,551,615)
b. Floating Point Numbers
float32 (precision of 6 to 9 decimal digits)
float64 (precision of 15 to 17 decimal digits)
w. Complex Numbers
complex64 (composed of two float32 values)
complex128 (composed of two float64 values)
d. Strings
string: immutable sequence of Unicode characters.
and. Booleans
bool: can be true or false.
2. Composite Types
the. Arrays
Contain a fixed number of elements of a single type.
var nums [5]int // Array of 5 integers
b. slices
Represent dynamic slices of arrays.
nums := []int{1, 2, 3, 4, 5}
w. Maps
Key-value structure.
var m map[string]int
// Map from string to integer
d. Structures
Collection of grouped fields.
type Person struct {
String name
Internal age
}
and. Pointers
Reference to memory address.
var p *int
f. Interfaces
Defines behavior and allows abstraction.
type Interface Animal {
String Speak()
}
3. Special Types
the. Functions
Types that represent functions.
var f function(int, int) int
b. Channels
Used for communication between goroutines.
var ch chan int
w. void interface
Void interface (interface{}) that can store any type.
var xinterface{}
d. Unsafe pointer
Pointers that bypass type checking. Used in advanced scenarios.
import "unsafe"
var p unsafe.Pointer
Resume
Data types in Go are designed to be simple and efficient, with a focus on type safety and performance. They cover most common use cases and offer enough flexibility for complex applications.
🚀 link: https://go.dev/tour/basics/11
Top comments (0)