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.


  • 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
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 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]
cricketer = append(cricketer, "McGrath")
view raw slicesDemo.go hosted with ❤ by GitHub
[Sachin Ponting Waugh]
[Zaheer Waqar Lee]
[Zaheer Waqar Lee]
[ Sachin Ponting Waugh Zaheer Waqar Lee McGrath]


  • 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"
delete(students, 1)
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
map[0:Harsh 1:Yash]
Roll number of Harsh is 0
Roll number of Jain is 1


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)
ABC lives in XYZ and studies in X
  • If all the fields of struct are comparable, struct is comparable


  • 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"
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 {
view raw json-demo.go hosted with ❤ by GitHub
map[key1:value1 key2:value2 key3:value3] {"key1":"value1","key2":"value2","key3":"value3"} 
map[key1:value1 key2:value2 key3:value3]


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

Up ↑

%d bloggers like this: