GO – Types (part 1)

Pointers

264px-Pointers.svg

 

  • GO has pointers as in C. A pointer holds the memory address of a value.
  • & operator generates a pointer to its operand
  • ‘*’ operator refers to the underlying value of the pointer

 

package main

import "fmt"

func main() {
  i := 24
  p := &i
  fmt.Println(p)
  *p = *p/2      // This is called "dereferencing" or "indirecting".
  fmt.Println(i)
}

Output:

0xc420082010
12

Structs

Struct is a collection of fields. The fields can be accessed using a dot(.)

package main

import "fmt"

type Emp struct {
   Name string
   Age int
   ID string
}

func main() {
  e1 := Emp{"John",23,"1234"}
  fmt.Println(e1)
  e1.Age = 30
  fmt.Println(e1)
}

Pointers to Structs

Stucts can be accessed through a struct pointer. The struct field (Name) of a struct pointer (sp) can be accessed as sp.Name

package main

import "fmt"

type Emp struct {
   Name string
   Age int
   ID string
}

func main() {
  e1 := Emp{"John",23,"1234"}
  sp := &e1
  fmt.Println(sp.Name)
  sp.Name = "James"
  fmt.Println(sp.Name)
}

Struct Literals

Struct Literal is a newly allocated struct value. It is not required to allocate values to all the fields. Just a few fields can be used. The order of assignment also doesn’t matter. The unassigned values are implicitly assigned the Zero value of its type.

package main

import "fmt"

type strLit struct {
   X int
   Y string
   Z bool
}

var (   //Struct Literals:
  s1 = strLit{}   
  s2 = strLit{X:1,Y:"golang"}
)

func main() {
  fmt.Println(s1)
  fmt.Println(s2)
}

Arrays:

Array is of Fixed size in GO. It is declared as abc [n]T where abc is an Array of n values with the type T.

func main() {
   var a [2]string
   a[0] = "Dobby is a"
   a[1] = "free elf"
   fmt.Println(a)
   fmt.Println(a[0],a[1])
   
   vowels := [6]string{"a","e","i","o","u"}
   fmt.Println(vowels)
}

Slice literal

A slice is a dynamicalled sized Array.

  • A slice does not store any data. Slices are like references to an Array. Modifying an element in the slice results in modifying the underlying array.
  • A slice literal creates an array and builds a slice that references it.
func main() {
   vowels := [6]string{"a","e","i","o","u"}
   var sub []string = vowels[:3]
   fmt.Println(sub)
   
   t := []int{1,2,3,4}
	 fmt.Println(t)
}
  • When slicing, 0 is taken as the default low bound and the length of the slice as the default high bound (similar to Python Arrays). Ex. For the array: t [10]int these expressions are equivalent: t[:], t[:10], t[0:10], t[0:]
  • Slice Length: Number of elements it contains. Can be obtained using the function len(t)
  • Slice capacity: Maximum number of elements the slice can contain. Can be obtained using the function cap(t)
  • Nil Slice: The slice’s len() and cap() is zero. The ‘zero’ value of a slice is ‘Nil’
func main() {
  var s[]int 
  fmt.Println(s, len(s), cap(s))
    if s == nil {
      fmt.Println("nil!")
    }
}

Make – for creating slices

Dynamically sized arrays can be created using the ‘make‘ function. The length and capacity of the slice can be passed as arguments to the ‘make’ function.

package main

import "fmt"

func main() {
  a := make([]int,4,6)
  printSlice("a",a)
	
  b := make([]int,2,6)
  printSlice("b",b)
	
  c:= b[:4]
  printSlice("c",c)
	
  d:= b[1:]
  printSlice("d",d)	
}

func printSlice(s string, x []int) {
  fmt.Printf("%s len=%d cap=%d %v\n",
		s, len(x), cap(x), x)
}

Slices of slices:

Slices can contain other slices too.

package main

import "fmt"

func main() {
	sliceEx := [][]string{
	[]string{"Superman","Batman","Wonder Woman"},
	[]string{"Hulk","Ironman","Spiderman","Captain America"},
	[]string{""},
	}
	fmt.Println(sliceEx)
}

Appending to a slice:

New elements can be appended to a slice using the ‘append‘ function. More than one element can be appended in a single function call. The first parameter is the slice followed by the elements.

func main() {
 var t []int
 
 t = append(t,0)
 t = append(t,1,2,3,4)
}

 

Previous post: Go – Flow Control

Next Post: GO – Types (part 2)

 

Advertisement

3 thoughts on “GO – Types (part 1)

  1. Pingback: GO – Types (part 2) – Reverie

  2. Pingback: GO – Flow Control – Reverie

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s