Skip to content
Learn Netverks

Lesson

Step 13/36 36% through track

methods-go

Methods

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Methods: the syntax, patterns, and safety habits you need before advancing in Go.

Teams still ship Methods in Go codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Methods in contexts like: Kubernetes ecosystem tools, cloud APIs, and CLI utilities.

Write Go in main.go with package main and func main(), click Run on server—the dev runner runs go run main.go; use fmt.Println for output (requires Go toolchain; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Methods are functions with a receiverfunc (p Person) Greet() string. Receivers can be value or pointer; pointer receivers can mutate and avoid copying large structs.

Value vs pointer receivers

func (p Person) String() string { ... }       // value receiver
func (p *Person) Birthday() { p.Age++ }     // pointer receiver

Be consistent: if any method needs a pointer receiver, usually use pointer receivers for all methods on that type.

Method sets and interfaces

A type satisfies an interface if it has the required methods. Pointer and value receivers affect which interfaces a value satisfies—*T often needed when methods use pointer receivers.

Important interview questions and answers

  1. Q: When use pointer receiver?
    A: When mutating the receiver or avoiding copy of large structs; also when methods must match pointer-based interface satisfaction.
  2. Q: Methods on non-struct types?
    A: Any named type (except pointer to built-in) can have methods—e.g. type MyInt int.

Self-check

  1. Which receiver type can modify struct fields?
  2. Can you define methods on int directly?

Pitfall: Mixing value and pointer receivers inconsistently can break interface satisfaction—pick one style per type when possible.

Interview prep

Pointer receiver when?

When mutating the receiver or avoiding copy of large structs; be consistent across methods.

Methods on int?

Not on bare int—define a named type like type MyInt int first.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Pointer receiver why?
  • Value receiver copy?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump