Structs in the Go Programming Language




Welcome to part 6 of the Go programming tutorial series. In the previous tutorial, we showed a simple web server example using Go, almost all of which was done using concepts we were already familiar with. That said, we had some seemingly custom types:

func index_handler(w http.ResponseWriter, r *http.Request) {
	...
}

In the programming language of Go, there are no Classes.

*gasp*

Instead, we have structs, and you can have methods by defining methods on the structs. You can think of it like they're "associated" with the struct. That probably doesn't help much to understand though, so let's see some examples. Let's consider that we're building some video game that has a car. In something like Python, we'd make a Car class, defining some attributes, and then methods for doing calculations and actions with this car. In Go, instead, we create a car type using a struct.

type car struct {
	gas_pedal uint16     //min: 0,      max: 65535
	brake_pedal uint16   //min: 0,      max: 65535
	steering_wheel int16 //min: -32768  max: 32768
	top_speed_kmh float64
}

Now, to use this struct, we can do:

a_car := car{gas_pedal: 16535, brake_pedal: 0, steering_wheel: 12562, top_speed_kmh: 225.0}

You can also abbreviate it by passing the parameters in order:

a_car := car{22314,0,12562,225.0}

If you're hard-coding values like we are here, it might be a good idea to use the named values. Chances are, however, in your actual code you'd be passing some variables here. If the variables have decent naming, you can just use the short-hand method.

a_car := car{22314,0,12562,225.0}

...is going to be hard to read later and know what's going on.

a_car := car{gas_pedal_pos,brake_pedal_pos,steering_wheel_pos,top_speed_kmh}

...is legible.

So, we could have a main function like:

func main() {
	//a_car := car{gas_pedal: 16535, brake_pedal: 0, steering_wheel: 12562, top_speed_kmh: 225.0}
	a_car := car{22314,0,12562,225.0}
	fmt.Println("gas_pedal:",a_car.gas_pedal, "brake_pedal:",a_car.brake_pedal,"steering_wheel:",a_car.steering_wheel)
}

Notice how we have defined a_car to be acar struct, and then we can reference the struct fields with a dot, like access attributes in a traditional class. The full script:

package main

import "fmt"

type car struct {
	gas_pedal uint16     //min: 0,      max: 65535    16bit
	brake_pedal uint16   //min: 0,      max: 65535
	steering_wheel int16 //min: -32768  max: 32768
	top_speed_kmh float64 //what's our top speed?
}

func main() {
	//a_car := car{gas_pedal: 16535, brake_pedal: 0, steering_wheel: 12562, top_speed_kmh: 225.0}
	a_car := car{22314,0,12562,225.0}
	fmt.Println("gas_pedal:",a_car.gas_pedal, "brake_pedal:",a_car.brake_pedal,"steering_wheel:",a_car.steering_wheel)
}

Okay, that's easy enough, but what if we want to actually have something like a method rather than just simply accessing some values? We'll show some methods in the next tutorial.

The next tutorial:





  • Introduction to the Go Programming Language
  • Go Language Syntax
  • Go Language Types
  • Pointers in Go Programming
  • Simple Web App in Go Programming
  • Structs in the Go Programming Language
  • Methods in Go Programming
  • Pointer Receivers in Go Programming
  • More Web Dev in Go Language
  • Acessing the Internet in Go
  • Parsing XML with Go Programming
  • Looping in Go Programming
  • Continuing our Go Web application
  • Mapping in Golang
  • Mapping Golang sitemap data
  • Golang Web App HTML Templating
  • Applying templating to our Golang web app
  • Goroutines - Concurrency in Goprogramming
  • Synchronizing Goroutines - Concurrency in Golang
  • Defer - Golang
  • Panic and Recover in Go Programming
  • Go Channels - Concurrency in Go
  • Go Channels buffering, iteration, and synchronization
  • Adding Concurrency to speed up our Golang Web Application