Structs

Go isn't quite an OOP language, but the closest thing to OOP in go is structs which we will cover in this section.

Golang structs (or structures) are used to create a collection of various datatypes into one datatype. That is, they are a typed collections of fields. They are similar to classes in object oriented programming, but they do not possess features like polymorphism and inheritance in the same way that classes in OOP languages like Java do. In this section, we will go over the basics of structs and how they can be used to build complex applications in golang.

The basic Idea

We can define a struct as follows

// declare the type of the struct
type Book struct {
  title          string
  year_published int    // set the fields in the struct
  author         string // the fields can be of different types
  price          float64
}

We declare a struct using the type <StructName> struct { ... } syntax. Note that the field values can be of any type (including other structs).

Usage

We can then use the structs in our program as follows:

package main

import "fmt"

func new_book(title string, year_published int, author string, price float64) *Book {
  book := Book{title: title}
  book.author = author
  book.year_published = year_published
  book.price = price
  return &book
}

func main() {
  // create a new struct
  book := Book{title: "Blue Nights", year_published: 2017, author: "Joan Didion", price: 29.99}
  fmt.Println(book)
  
  // access the fields of the struct
  fmt.Println(book.title)
  fmt.Println(book.year_published)
  fmt.Println(book.author)
  fmt.Println(book.price)
  
  // change the value of a field
  book.price = 19.99
  fmt.Println(book.price)
  
  // create a new struct with the new keyword
  book2 := new(Book)
  book2.title = "Life of Pi"
  book2.year_published = 2001
  book2.author = "Yann Martel"
  book2.price = 9.99
  fmt.Println(book2)
  
  // create a new struct with a function
  // this is somewhat similar to a constructor in other languages
  // however, it is considered more idiomatic to do this in Go
  book3 := new_book("Utilitarianism", 1861, "John Stuart Mill", 9.99)
  fmt.Println(book3)
}

Struct methods

Similar to class methods in OOP languages, structs also support method functions. Here are a few examples

package main

import "fmt"

// declare the type of the struct
type Book struct {
  title          string
  year_published int    // set the fields in the struct
  author         string // the fields can be of different types
  price          float64
}

// struct methods
func (book *Book) double_price(price float64) {
  book.price = price * 2
}
// print a basic bio of the book
func (book *Book) book_bio() string {
  fmt.Println("A bio of the book")
  return fmt.Sprintf(
    "The book '%s' was written by %s in %d and costs $%.2f", 
    book.title, book.author, book.year_published, book.price
  )
}

func book_vendor() {
  // create a new struct
  book := Book{title: "Blue Nights", year_published: 2017, author: "Joan Didion", price: 29.99}
  fmt.Println(book)
  // double the price of the book
  fmt.Println("Double the price of the book")
  book.double_price(book.price)
  fmt.Println(book.price)
  // print the book bio
  fmt.Println("Book Bio")
  fmt.Println(book.book_bio())
}

Last updated