🚀 Get 25% off access to all my premium courses - use discount code FUNCMAIN at checkout - view the pricing page now!

Video:

Methods in Go

June 4, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.

In this video, we are going to be looking at structs in Go and what they are used for.

Overview

package main

import "fmt"

type Engineer struct {
	Name    string
	Age     int
	Project Project
}

type Project struct {
	Name         string
	Priority     string
	Technologies []string
}

func (e Engineer) Print() {
	fmt.Println("Engineer Information")
	fmt.Printf("Name: %s\n", e.Name)
	fmt.Printf("Age: %d\n", e.Age)
	fmt.Printf("Current Project: %s\n", e.Project.Name)
}

func (e *Engineer) UpdateAge() {
	e.Age += 1
}

func (e *Engineer) GetProjectPriority() string {
	return e.Project.Priority
}

func main() {
	fmt.Println("Methods Tutorial")
	engineer := Engineer{
		Name: "Elliot",
		Age:  27,
		Project: Project{
			Name:         "Beginner's Guide to Go",
			Priority:     "High",
			Technologies: []string{"Go"},
		},
	}

	engineer.UpdateAge()
	engineer.Print()

	fmt.Println(engineer.GetProjectPriority())

}

Output

$ go run main.go
Methods Tutorial
Engineer Information
Name: Elliot
Age: 28
Current Project: Beginner's Guide to Go
High