Golang Working with structs

Golang Working with structs

In golang structs are primittive data types used to create a collection of fields. These are useful for gouping the data. We can compare these with python/java class. Golang is not a object oriented programming language so, we didn't have a concept of class. but, we can achieve the similar functionality with structs and interfaces. we can check about it in the next article.

Note: use https://play.golang.org/ to execute the golang code online.

syntax to create a struct

Let's create simple struct which represents a Person with data first name, last name and age.

type Person struct{
    FirstName   string
    LastName    string
    Age         int
}

Create object of struct and add values to the struct fields

Let's create simple struct for a person and create an object to it.

package main

import "fmt"

type Person struct{
    FirstName   string
    LastName    string
    Phone       int
}

func main(){

    anji := Person{FirstName: "Anji", LastName: "B", Phone: 1234567890}
    fmt.Println(anji)
    // output: {Anji B 1234567890}

    // create object with new keyword
    user := new(Person)

    jennifer := Person{}
    jennifer.FirstName = "Jennifer"
    jennifer.LastName = "D"
    jennifer.Phone = 1233211230

    fmt.Println(jennifer)
    // output: {Jennifer D 1233211230}
}

Accessing the fields of a struct

  • We use dot notation to access the fields of a struct
  • Let's access the fields of a struct. see an example below.
package main

import "fmt"

type Person struct{
    FirstName   string
    LastName    string
    Phone       int
}

func main(){
    anji := Person{FirstName: "Anji", LastName: "Batta", Phone: 1234567890}
    fmt.Println("First name: ", anji.FirstName)
    // output: Anji
    fmt.Println("Last name: ", anji.LastName)
    // output: Batta
}

Updating the fields of a struct

  • Let's see an example how we can update a field of struct object
package main

import "fmt"

type Address struct {
    name, street, city, state string
    Pincode int
}

func main(){
    addr := Address{
        name: "3-5, ABC",
        street: "Golden Street",
        state: "Telangana",
        Pincode: 500072}

    // Let's update the pincode
    addr.Pincode = 1001
    fmt.Println("Pincode: ", addr.Pincode)
    // output: 1001
}

Anonymous struct

we can crate the anonymous structs like below witout defining the struct.

package main

import (
    "fmt"
)

func main() {

    pizza := struct {
        name string
    }{
        name: "Pizza",
    }

    fmt.Println(pizza)
    // output: {Pizza}
}

compare two struct objects in golang

we use == operator to compare the struct objects

package main

import (
    "fmt"
)

type Student struct {
    name string
}

func main() {
    s1 := Student{"John"}
    s2 := Student{"John"}

    if(s1 == s2) {
        fmt.Println("They are equal")
    } else {
        fmt.Println("They are not equal")
    }
    // output: "They are equal"
}

Note: creating a struct means we are literally creating a new or a custom data type in golang so, we can use the newly created just like any other primittive data type golang.