GoLang Journey – Composite Types

You might want to read the previous post in the series.

Composite types created by combining the basic types like int, float etcetra.

Arrays

  • An array is a fixed length sequence of zero or more elements of particular type
  • Because of the fixed length constraints, Arrays are rarely used in Go
  • if “…” appears in place of length, that means the length of array is determined by number of initialisers
  • Size of array is part of it’s type, so [7] int is different from 9[int]
func main() {
var a [3]int = [3]int{1,2,3}
fmt.Println("Element at index is",a[1])
fmt.Println("Length of array is",len(a))
for i, v := range(a){
fmt.Printf("Index is %d, and value is %d \n",i,v)
}
q := […]int{1, 2, 3}
fmt.Println("Length of array is",len(q))
}
view raw array.go hosted with ❤ by GitHub
Output:
Element at index is 2
Length of array is 3
Index is 0, and value is 1
Index is 1, and value is 2
Index is 2, and value is 3
Length of array is 3

Slices

Slices are variable length sequences of elements the same type. A slice type is []T where T is the type of element.

  • A slice is a dynamically-sized flexible view into the elements of an array
  • A slice has three components: A pointer, length and a capacity
  • Unlike Arrays, slices are not comparable. We can not use “==” operator to test whether both slices have same elements or not
  • The built-in append function append s items to slices
func main() {
cricketer := []string{1:"Sachin", 2:"Ponting", 3: "Waugh", 4: "Zaheer", 5: "Waqar", 6:"Lee"}
batsman := cricketer[1:4]
bowler := cricketer[4:7]
fmt.Println(batsman)
fmt.Println(bowler)
cricketer = append(cricketer, "McGrath")
fmt.Println(bowler)
fmt.Println(cricketer)
}
view raw slicesDemo.go hosted with ❤ by GitHub
Output:
[Sachin Ponting Waugh]
[Zaheer Waqar Lee]
[Zaheer Waqar Lee]
[ Sachin Ponting Waugh Zaheer Waqar Lee McGrath]

Maps

  • Map is an unordered collection of key and value pair
  • Keys are distinct
  • Update, insert, delete operations are in constant time
  • Key must be comparable using “==”
  • Maps can not be compared with each other
func main() {
students := make(map[int]string)
students[0] = "Harsh"
students[1] = "Yash"
fmt.Println(students)
delete(students, 1)
fmt.Println(students)
students[1] = "Jain"
for roll, name := range students{
fmt.Printf("Roll number of %s is %d \n",name, roll)
}
}
view raw mapsDemo.go hosted with ❤ by GitHub
Output:
map[0:Harsh 1:Yash]
map[0:Harsh]
Roll number of Harsh is 0
Roll number of Jain is 1

Struct

A struct is an aggregate data type that groups together zero or more named values of arbitrary types as a single entity. Like student data containing it’s id, name, class etc.

package main
import "fmt"
type Student struct {
Name string
Address string
Class string
}
func main() {
s := Student{"ABC", "XYZ", "X"}
fmt.Printf("%s lives in %s and studies in %s",s.Name, s.Address, s.Class)
}
Output:
ABC lives in XYZ and studies in X
  • If all the fields of struct are comparable, struct is comparable

JSON

  • JavaScript Object Notation (JSON) is a standard notation for sending and receiving structured information
  • Converting from Go data structure to JSON is called marshaling
  • Converting from JSON to Go data structure is called unmarhsaling
package main
import "fmt"
import "encoding/json"
var m map[string]string
var mDup map[string]string
func main() {
m = make(map[string]string)
m["key1"] = "value1"
m["key2"] = "value2"
m["key3"] = "value3"
fmt.Println(m)
data, _ := json.Marshal(m)
fmt.Printf("%s\n", data)
dataIndent, _ := json.MarshalIndent(m, "", " ")
fmt.Printf("%s\n", dataIndent)
err := json.Unmarshal(dataIndent, &mDup)
if err != nil {
fmt.Println(err)
}
fmt.Println(mDup)
}
view raw json-demo.go hosted with ❤ by GitHub
Output:
map[key1:value1 key2:value2 key3:value3] {"key1":"value1","key2":"value2","key3":"value3"} 
{
"key1":"value1",
"key2":"value2",
"key3":"value3"
}
map[key1:value1 key2:value2 key3:value3]

Reference

Download Go

The Go Programming Language – Chapter 4 – Alan Donovan

If you liked this article and would like one such blog to land in your inbox every week, consider subscribing to our newsletter: https://skillcaptain.substack.com

One thought on “GoLang Journey – Composite Types

Add yours

Leave a Reply

Blog at WordPress.com.

Up ↑