Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

structs-go

Structs

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

This lesson

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

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

You will apply Structs 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.

Structs are Go's primary way to group fields—like classes without inheritance. Compare with struct in C# or plain data classes in Java records.

Defining and using structs

type Person struct {
    Name string
    Age  int
}
p := Person{Name: "Ada", Age: 36}

Struct literals require field names for clarity when omitting fields. Zero value is a struct with zero-valued fields.

Embedding (composition)

Anonymous struct embedding promotes fields and methods—Go favors composition over inheritance. Embedded types must be unexported or exported consistently.

Important interview questions and answers

  1. Q: Classes in Go?
    A: No classes—structs plus methods and interfaces replace OOP hierarchies.
  2. Q: Struct embedding vs inheritance?
    A: Embedding composes behavior without subtype polymorphism by default—interfaces provide polymorphism.

Self-check

  1. How do you create a Person with Name "Ada"?
  2. What is the zero value of a struct?

Tip: Go favors composition via embedded structs over inheritance—compare with class hierarchies in Java.

Interview prep

Classes in Go?

No classes—structs with methods and interfaces replace inheritance hierarchies.

Struct embedding?

Anonymous embedding promotes fields/methods—composition over inheritance.

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

  • Struct tags?
  • Embedding vs inherit?

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