I have been in DevOps related jobs for past 6 years dealing mainly with Kubernetes in AWS and on-premise as well. I spent quite a lot …
:date_long | 1 min Read
Go pointers
package main
import (
"fmt"
// "strconv"
// "math"
// "reflect"
// "net/http"
// "log"
)
func simple() {
a := 42
// b will be a brand new variable with it's place in memory
b := a
fmt.Printf("a: %v, b: %v\n", a, b)
a = 27
fmt.Printf("a: %v, b: %v\n", a, b)
}
func withPointer() {
var a int = 42
var b *int = &a
fmt.Printf("a: %v, b(memory location): %v, dereferencing: %v\n", a, b, *b)
a = 27
fmt.Printf("a: %v, b(memory location): %v, dereferencing: %v\n", a, b, *b)
}
func aritmwithPointer() {
a := [3]int{1, 2, 3}
b := &a[0]
c := &a[1]
fmt.Printf("a: %v, b: %v, c: %v\n", a, b, c)
fmt.Printf("a: %v, b: %v, c: %v\n", a, *b, *c)
fmt.Printf("a: %v, b: %p, c: %p\n", a, b, c)
var ms myStruct
ms = myStruct{foo: 42}
// var ms *myStruct
// ms = &myStruct{foo: 42}
fmt.Println(ms.foo)
}
type myStruct struct {
foo int
}
func main() {
// simple()
// withPointer()
aritmwithPointer()
}