Golang Tutorials

Structure Basics

Let's start with a structure definition. A structure is a user-define data type that can be used to combine different data types and methods together. Even other structures!

Those coming from an object oriented programming paradigm will feel right at home using golang structs... once you get the syntax and best practices down.

Here is a code example of a structure called "person".

package main

// Person structure
// keyword structurename keyword
type person struct {
	firstName  string
	lastName   string
	age        int
	address    string
	methodUsed int
}

func main() {

}

Next we are going to add some functions that use our new structure

// func keyword to declare function, followed by a methodname(), then the returntype
func createEmptyPerson() person {
	return person{}
}

// DeclarePerson1 returns a struct type of person, using the first method of instantiation.
func declarePerson1() person {
	return person{"House", "Cat", 34, "Surfin' USA", 1}
}

// DeclarePerson2 returns a struct type of person, using the second method of instantiation.
func declarePerson2() person {
	return person{
		firstName:  "House",
		lastName:   "Cat",
		age:        34,
		address:    "Surfin' USA",
        // below is an extra comma is needed when you do line by line assignment otherwise it won't compile
		methodUsed: 2, // <=
	}
}

// DeclarePerson3 returns a struct type of person, using the third method of instantiation.
func declarePerson3() person {
	var newPerson person

	newPerson.firstName = "House"
	newPerson.lastName = "Cat"
	newPerson.age = 34
	newPerson.address = "Surfin' USA"
	newPerson.methodUsed = 3

	return newPerson
}

Now we are going to call our methods from main() and check the outputs.

func main() {
	emptyPerson := createEmptyPerson()

	// print format with argument %v as value, so print emptyPerson
	fmt.Printf("Empty Person: %v", emptyPerson)
	fmt.Println()
	fmt.Println(declarePerson1())
	fmt.Println(declarePerson2())
	fmt.Println(declarePerson3())
}

Additional Links

Everything Together

package main

import "fmt"

// Person structure
type person struct {
	firstName  string
	lastName   string
	age        int
	address    string
	methodUsed int
}

func main() {
	emptyPerson := createEmptyPerson()

	// print format with argument %v as value, so print emptyPerson
	fmt.Printf("Empty Person: %v", emptyPerson)
	fmt.Println()
	fmt.Println(declarePerson1())
	fmt.Println(declarePerson2())
	fmt.Println(declarePerson3())
}

// func keyword to declare function, followed by a methodname(), then the returntype
func createEmptyPerson() person {
	return person{}
}

// DeclarePerson1 returns a struct type of person, using the first method of instantiation.
func declarePerson1() person {
	return person{"House", "Cat", 34, "Surfin' USA", 1}
}

// DeclarePerson2 returns a struct type of person, using the second method of instantiation.
func declarePerson2() person {
	return person{
		firstName:  "House",
		lastName:   "Cat",
		age:        34,
		address:    "Surfin' USA",
		methodUsed: 2,
	}
}

// DeclarePerson3 returns a struct type of person, using the third method of instantiation.
func declarePerson3() person {
	var newPerson person

	newPerson.firstName = "House"
	newPerson.lastName = "Cat"
	newPerson.age = 34
	newPerson.address = "Surfin' USA"
	newPerson.methodUsed = 3

	return newPerson
}

Play.Golang.Org Sandbox

I have embedded this source location for you to run and see outputs.