Monday, July 13, 2015

Go Structs

The developers who don't like OOP and are yet to figure out the precise difference between a class & object can breath easy now. 

Go has got rid of Classes.

Is Go an Object Oriented Language?

Before you read my version, Find the official answer here. In a strict sense, personally I believe that Go doesn't have the essential attributes to be called an OOP language. Go doesn't have the concept of Object, Inheritance Polymorphism. Does this make things simple? You may choose to differ but I think, Yes it does. That's one of the various reasons I like Go - it's simple & yet so powerful. 

What are Structs?

Go has structs which are behaviorally similar to classes and can be associated with Methods. Struct is a custom type which represents a real world entity with its properties, and each property may have its own type. 
  • Structs are value types.
See the following code sample where a struct named Person has 2 fields (properties) - name of type string and age of type int.


RefCode#1.1

package main
import "fmt"

type Person struct {
 name string
 age  int
}

func main() {
 p := Person{}
 fmt.Println("Default values for Person is: ", p)
}

Output RefCode#1.1


Default values for Person is:  {0}

Play with the above code

As we've not provided any value to the struct fields name and age the output is initialized with default values i.e. empty string or zero; depending on the data type of the fields.

Another sample RefCode#1.2


package main

import "fmt"

type Person struct {
 name string
 age  int
}

func main() {
 p := Person{"Steve", 56} // assign value in the order in which they are defined
 fmt.Println("Person's name & age is: ", p)
}

Output RefCode#1.2


Person's name & age is:  {Steve 56}

Play with the above code

In the following code, we've provided a value that is reflected in the output.
sample RefCode#1.2 can be rewritten as:

Sample RefCode#1.3


package main

import "fmt"

type Person struct {
 name string
 age  int
}

func main() {
 p := Person{age: 56, name: "Steve"} //assign value by variable name and :
 fmt.Println("Person's name & age is: ", p)
 fmt.Println("Person's age is: ", p.age)
}

The output of the above sample:

Person's name & age is:  {Steve 56}
Person's age is:  56

Play with the above code.

If you've observed the above code samples carefully you might have noticed how a struct is initialized. If you're new to programming you can read the following section.

Initialization of a Struct

An instance of type person can be created by any one of the following two ways:

1. var p Person
--- This creates a local person variable with default values set to zero i.e. age = 0 and name = ""  

"" represents empty string

2. p := new(Person)

This allocates default values for the struct fields age and name. It returns a pointer. 

Assigning Values to Struct Fields

Use any of the two ways described below:

1. p := Person{age: 29, name: "Basant"} 

        --- You can change the order of the fields, can also be written as follows:
        p := Person{name: "Basant", age: 29}

2. p := Person{29, "Basant"}

        --- You must know the order of the field and maintain it. 

Accessing Struct Fields

Fields are accessed using the . operator (dot operator).

fmt.Println("Person's age is: ", p.age)

The above line of code is used in  RefCode#1.3.

Did you like this? Is it beginners friendly? Please spread the word about it.


No comments:

Post a Comment