Introducing Structures and Methods

Follow the latest of Whizdumb’s journey here.

Like in all programming languages, Go also provides a way to create your own data types by combining two or more basic types.

To do that Go provide us with struct keyword. Lets try and create a new type “Person”. All we need to do is

type Person struct {} 

Lets add some fields to person

type Person struct {
  FirstName, MiddleName, LastName string 
}

Now lets create an instance for this Person

var p Person

This creates an instance of Person referred by p and initialises all fields in Person to empty strings. If we try to print values for person p, we will get empty strings. Following code demonstrates that.

package main
import "fmt"
type Person struct {
  FirstName, MiddleName, LastName string 
} 
func main() {
	var p Person
	fmt.Println("The person name is ", p.FirstName, p.MiddleName, p.LastName)
}

This gives output

The person name is    

Here also notice how we accessed the fields of Person using “.” (dot) operator.

We can also use the new function to create variable.

p := new(Person)

Thing to note here is that it will return a pointer to the Person, so p here in essence is pointer to the Person struct. p is of type *Person

Now lets try to create a new variable and set some values for name of the person

 p := Person{FirstName:"dumb", LastName:"whiz"}

If we try to print the names now, we will get the person name. Here is how the program looks like with output

package main

import "fmt"

type Person struct {
  FirstName, MiddleName, LastName string 
} 
func main() {
	p := Person{FirstName:"dumb", LastName:"whiz"}
	fmt.Println("The person name is",p.FirstName,p.MiddleName, p.LastName)
}

output :

The person name is dumb  whiz

We can create a function which accepts person as a parameter and prints the name. But, what if we wanted to invoke a function on Person type, like p.printName() where p is a variable of Person type.

Go provides a way to “bind” functions to a particular type and such functions are knowns as methods. Lets see how we do it

func (p *Person) printName() {
    fmt.Println("The person name is",p.FirstName,p.MiddleName, p.LastName)
} 

In the above code we define a method printName() which accepts no parameter but can be invoked by a variable of type Person.

Here is the complete code sample

package main

import "fmt"

type Person struct {
  FirstName, MiddleName, LastName string 
}
func (p *Person) printName() {
    fmt.Println("The person name is",p.FirstName,p.MiddleName, p.LastName)
} 
func main() {
	p := Person{FirstName:"dumb", LastName:"whiz"}
	p.printName()
}

Neat!

~~ Whizdumb ~~

Advertisements

One thought on “Introducing Structures and Methods

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s