Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
In Go, data types specify the type of data a variable can hold. Understanding these types is fundamental for any Go programmer. Let's dive into the various data types in Go:
int8
, int16
, int32
, int64
, and int
(size depends on the platform: 32 or 64 bits)uint8
(also byte
), uint16
, uint32
, uint64
, and uint
uintptr
(an integer type that can store a pointer)var i int = 42 var u uint = 42
float32
float64
var f float32 = 3.14
complex64
complex128
var c complex64 = 1 + 2i
The bool
type represents true
or false
values.
var isTrue bool = true
A string
represents a sequence of characters:
var str string = "Hello, Gophers!"
Pointers hold the memory address of another variable:
var x int = 10 var p *int = &x
Arrays are sequences of elements of the same type:
var arr [5]int = [5]int{1, 2, 3, 4, 5}
Slices are similar to arrays but with a dynamic size:
slice := []int{1, 2, 3, 4, 5}
Maps are collections of key-value pairs:
m := map[string]int{ "apple": 5, "banana": 2, }
Structs are collections of fields with different data types:
type Person struct { Name string Age int } var p Person = Person{Name: "Alice", Age: 30}
Channels are used for communication between goroutines:
ch := make(chan int)
In Go, functions are first-class citizens and can be assigned to variables:
func sayHello() { fmt.Println("Hello!") } var helloFunc func() = sayHello
Interfaces define a contract (set of methods), and any type that satisfies this contract implicitly implements the interface:
type Writer interface { Write([]byte) (int, error) }
Go has a rich set of data types that cater to different programming needs, from basic types like integers and floats to more complex types like maps and channels. Understanding and effectively using these types is essential for writing efficient and readable Go programs.
Basic Data Types in Golang:
int
, float64
, bool
, string
, and complex
.package main import "fmt" func main() { var integer int = 42 var floatingPoint float64 = 3.14 var boolean bool = true var text string = "Hello, Golang!" var complexNumber complex128 = 1 + 2i fmt.Println(integer, floatingPoint, boolean, text, complexNumber) }
Numeric Data Types in Golang:
int
, int8
, int16
, int32
, int64
), unsigned integers (uint
, uint8
, uint16
, uint32
, uint64
), and floating-point numbers (float32
, float64
).package main import "fmt" func main() { var integer int = 42 var unsignedInt uint = 42 var floatingPoint float64 = 3.14 fmt.Println(integer, unsignedInt, floatingPoint) }
String Data Type in Golang:
string
type represents sequences of characters.package main import "fmt" func main() { greeting := "Hello, Golang!" fmt.Println(greeting) }
Composite Data Types in Golang:
package main import "fmt" func main() { // Example using arrays, slices, maps, and structs // (refer to examples in points 5, 6, and 9) }
Arrays and Slices in Golang:
package main import "fmt" func main() { // Array var arr [3]int = [3]int{1, 2, 3} // Slice slice := []int{4, 5, 6} fmt.Println(arr, slice) }
Structs and Custom Data Types in Golang:
package main import "fmt" // Define a struct type Person struct { Name string Age int } func main() { // Create an instance of the struct person := Person{"Alice", 25} fmt.Println(person) }
Pointers and Reference Types in Golang:
package main import "fmt" func main() { // Pointer example var x int = 42 var ptr *int = &x // Reference type (slice) slice := []int{1, 2, 3} fmt.Println(*ptr, slice) }
Type Conversion in Golang:
package main import "fmt" func main() { var x int = 42 var y float64 = float64(x) fmt.Println(y) }
Golang Data Types and Their Sizes:
unsafe
package can be used to check the size of a data type.package main import ( "fmt" "unsafe" ) func main() { var x int fmt.Println("Size of int:", unsafe.Sizeof(x)) }