Simple Object Structure Intialization in Go Language

The Go Language is an elegant and powerful programming language that may look slightly strange at first. Although Go is an object-oriented language, it sits in a slightly different paradigm then that of other similar languages.

The intention of this article is to:

  • get you started with building your first object structure intialization in Go language.
  • explain two ways to initialize an object structure with a method and function.
  • a touch on pointers and local variables
  • a touch on public and private

First an Example

package main

import (
	"fmt" //This is a built-in library which makes printing to console, as well as several other functions
)

//NOTE: Note that "Example" is capitalized. it is made public if it is captalized and private if lowercase
type exampleStructure struct {//Define the data structure that will be used
	id int
	firstName, lastName string 
}

//Example Method 1
func (e *exampleStructure)initExample(id int, firstName, lastName string) {//Go docs recoommend grouping like terms whenever possible
	e.id = id
	e.firstName = firstName
	e.lastName = lastName
} 

//Example Function Init
func initExample(id int, firstName, lastName string) Example {
	example := new(exampleStructure)//Note: the ":" char is used in this initialization of a local variable. 
	example.id = id
	example.firstName = firstName
	example.lastName = lastName
	return *example// Note: the "*" char is used to reference the pointer of the sturcture 
	
}

func main() {// To run a go file, it must be the "main" package (See 1st line of code) and must contain a main method like this 

	//Using Example Method 1
	var example exampleStructure//here we set a variable with the "Example" structure
	example.initExample(1, "first", "last")//Then pass in the desired variables
	fmt.Println(example)
	fmt.Println("ID: ", example.id)


	//Using Example Function Init
	examp := initExample(1, "first", "last")//A new variable is not necessary but is being used for example purposes
	fmt.Println(examp)
	fmt.Println("ID: ", examp.id)

}

The best idea would be to take this code, look at the notes, and play with it! change stuff and see what happens.

Google fortunately provides a “playground” to play with code. If you do not have Go setup to use on your computer yet, you can play with this code here https://play.golang.org/p/8D_-8PsV7OE to play with changing the Object Structure Intialization in Go Language.

Further Explanation

Example Method 1

Methods are Functions that include an attached “Structure” or struct. If we look at its declaration:

func (e *Example)initExample(id int, firstName, lastName string) {
	e.id = id
	e.firstName = firstName
	e.lastName = lastName
} 

Note the (e *Example) portion. This is the structure that is attached, thus this method can not be executed without an “Example” struct. The “*” character is used to indicate a pointer reference. So a method like this takes the pointer reference from the variable given, and thus is able to add the appropriate data to the structure in the main method.

So when the method is called:

var example Example
example.initExample(1, "first", "last")

a variable must be created in order to properly use it. then the modifications of the data is modified by the pointed element.

Example Function Initialization (Init)

Another way that a Structure can be initialized is with a simple function. This is especially important, when using primitive data structures (int, string, bool, etc…), as primitives can not be used as a method structure attachment. This example should feel a bit more natural to other object oriented languages.

func initExample(id int, firstName, lastName string) Example {
	example := new(Example)
	example.id = id
	example.firstName = firstName
	example.lastName = lastName
	return *example	
}

when this structure is built, the function itself creates the structure, then fills it up with the provided variables, and finally instead of having 2 identical objects, it simply passes that pointer reference to the variable in main.

examp := initExample(1, "first", "last")
	fmt.Println(examp)
	fmt.Println("ID: ", examp.id)

What is the Difference?

In terms of efficiency, readability, and speed, there is no difference in both the example method and function. This answer changes slightly after initializing the structure. Furthermore, as you work with Go more, it will become more clear when a method should be used and when a function should be used.

Leave a Reply