Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

functions-go

Functions

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

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

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

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

Functions are first-class values in Go. Multiple return values are idiomatic—especially (result, error) pairs instead of exceptions like Java.

Syntax and multiple returns

func add(a, b int) int {
    return a + b
}
func div(a, b int) (int, error) {
    if b == 0 {
        return 0, fmt.Errorf("divide by zero")
    }
    return a / b, nil
}

Named result parameters (func f() (n int, err error)) are allowed—use sparingly for clarity.

Variadic and deferred calls

func sum(nums ...int) int accepts zero or more ints. Functions can be assigned to variables and passed as arguments—enabling simple functional patterns.

Important interview questions and answers

  1. Q: Multiple return values?
    A: Common for results plus errors; callers must handle or ignore explicitly—no hidden exceptions.
  2. Q: Exported function names?
    A: Capitalized names are exported (public) across packages; lowercase names are package-private.

Self-check

  1. How do you return two values from a function?
  2. What makes a function name exported?

Pitfall: Ignoring the error return with _ hides failures—handle or wrap errors explicitly, unlike unchecked exceptions in Java.

Interview prep

Multiple returns?

Common pattern—especially (result, error); callers must handle explicitly.

Exported names?

Capitalized identifiers are exported from a package; lowercase are package-private.

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

  • Multiple return?
  • Named returns?

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