Structs and methods in Go

Learn Go · Beginner · 11 min · After Functions, multiple returns and errors in Go

How do you group data and behaviour without classes?

  1. 1Guess

    package main
    
    import "fmt"
    
    type Point struct {
    	X, Y int
    }
    
    func (p Point) MoveRight() {
    	p.X = p.X + 10
    }
    
    func main() {
    	p := Point{X: 1, Y: 2}
    	p.MoveRight()
    	fmt.Println(p.X)
    }
    
    What does this program print?
  2. 2Read

    Structs

    A struct is a sequence of named fields, each with a type. Declare one with type Account struct { Owner string; Balance int }. Make a value with a literal that names the fields: Account{Owner: "Ada", Balance: 50}. Read and write a field with a dot: acc.Owner. Fields you leave out get their zero values.

  3. 3Code

    A struct literal and its fields

    1 / 2
    package main
    
    import "fmt"
    
    type Account struct {
    	Owner   string
    	Balance int
    }
    
    func main() {
    	acc := Account{Owner: "Ada", Balance: 50}
    	fmt.Printf("%+v\n", acc)
    	fmt.Println(acc.Owner)
    	acc.Balance = 80
    	fmt.Println(acc.Balance)
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    Methods

    A method is a function with a receiver written before its name: func (a Account) String() string. A value receiver gets a copy of the struct. A pointer receiver, func (a *Account) Deposit(n int), gets the address, so it can change the original. A method named String is used by fmt when it prints the value.

  5. 5Code

    A pointer method and a String method

    1 / 2
    package main
    
    import "fmt"
    
    type Account struct {
    	Owner   string
    	Balance int
    }
    
    func (a *Account) Deposit(n int) {
    	a.Balance += n
    }
    
    func (a Account) String() string {
    	return fmt.Sprintf("%s has %d", a.Owner, a.Balance)
    }
    
    func main() {
    	acc := Account{Owner: "Ada"}
    	acc.Deposit(30)
    	acc.Deposit(20)
    	fmt.Println(acc)
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    package main
    
    import "fmt"
    
    type Account struct {
    	Owner   string
    	Balance int
    }
    
    func (a Account) Deposit(n int) {
    	a.Balance += n
    }
    
    func main() {
    	acc := Account{Owner: "Ada"}
    	acc.Deposit(30)
    	fmt.Println(acc.Balance)
    }
    
    Deposit runs but the balance stays 0. What is the bug?
  7. 7Say it back

  8. 8Mission · 8 min

    Build a Counter type with a count field, a pointer method Increment and a String method. Call Increment three times and print the counter.

    go run .

Free AI coding tools to practise with

  1. AgentRouter Free Credits

    Free AI coding credits: $25 per daily check-in and $50 per invite

    No cardVerified

  2. Cursor Free Plan

    Cursor Agent with limited agent and chat requests, $0/month

    No cardVerified

  3. Cline Free Models

    Try select models free in Cline via rotating promotions with a usage quota

    No cardVerified

All free ai coding tools →

Common questions

Does Go have classes?
No. Go groups data in struct types and attaches behaviour with methods, which are functions with a receiver. You can define methods on any type you declare yourself. There is no class keyword and no inheritance; instead you build larger types from smaller ones and use interfaces to describe behaviour. For small programs a struct and a few methods cover most of what a class would do.
When should I use a pointer receiver in Go?
Use a pointer receiver when the method has to change the receiver, because a value receiver only gets a copy. The Go documentation notes that pointer methods can modify the receiver, while value methods work on a copy. Pointer receivers are also common for large structs, since passing an address avoids copying the whole value on every call.
How do I print a struct in Go?
Pass it to fmt.Println for a plain list of values, or use fmt.Printf with the %+v verb to include the field names, which helps when debugging. If your type has a String method, fmt calls it and prints its result instead. That lets you control exactly how a value of your type appears in output.

Sources

Measured with go1.27.4 darwin/arm64 on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer