Back to home
Go programming language logo

Go Online Compiler

Practice Go right here — no installs, no signups.

Go Compiler
Output
Code runs on the Play with Coding execution engine — your code is saved locally for next time.

About the Go online compiler

Go was designed at Google for services that must be simple to read and fast to build. It compiles in a blink, has garbage collection, and makes concurrency a first-class language feature through goroutines and channels. This online Go compiler runs your package main directly.

Practise slices and maps, structs and methods, interfaces, error returns as ordinary values, and concurrency with go func and channels. Go's compiler is strict — unused variables and imports are hard errors, which trains tidy code fast.

  • Learning goroutines and channels safely
  • Practising Go's explicit error-return style
  • Testing slice append and capacity behaviour
  • Writing small CLI-style exercises

Go sample programs you can run right now

Copy any snippet into the editor above and press Run. Each one is short on purpose — retype it from memory afterwards.

Structs and methods

package main

import "fmt"

type Student struct {
	Name  string
	Marks int
}

func (s Student) Passed() bool { return s.Marks >= 40 }

func main() {
	s := Student{"Aarav", 78}
	fmt.Println(s.Name, "passed:", s.Passed())
}

Methods hang off a receiver (s Student) rather than living inside the type body.

Goroutines and a channel

package main

import "fmt"

func main() {
	ch := make(chan string)
	go func() { ch <- "from a goroutine" }()
	fmt.Println(<-ch)
}

Receiving from an unbuffered channel blocks until the goroutine sends — that is how the program stays in sync.

Common Go errors and how to fix them

declared and not used

Why: Go rejects unused local variables.

Fix: Remove the variable or assign it to _ if it is intentionally ignored.

imported and not used

Why: An import is present but never referenced.

Fix: Delete the import line.

all goroutines are asleep - deadlock!

Why: Every goroutine is blocked on a channel with no sender or receiver.

Fix: Make sure something sends before you receive, or use a buffered channel.

Go syntax cheatsheet

ConceptSyntax
Package + mainpackage main\nfunc main() { }
Printfmt.Println(x)
Slicenums := []int{1, 2, 3}
Mapm := map[string]int{"a": 1}
Error returnv, err := doThing()
Goroutinego worker()

Go compiler FAQs

Do I need a go.mod file?

No. Single-file package main snippets run as-is here.

Are goroutines really concurrent here?

Yes, the program runs on a real Go runtime, so scheduling and channel semantics behave normally.

Keep going after the compiler

Running snippets builds speed; the 17-chapter course builds understanding. Work through the Go chapters, take the quiz, then claim your certificate.

Other compilers: JavaScript compiler, TypeScript compiler, Python 3 compiler, Java compiler, C compiler, C++ compiler, Rust compiler, HTML compiler, CSS compiler, SQLite compiler

Go tutorial: five steps from blank editor to working program

These steps cover the Go you need to read most production Go code: packages and main, explicit error returns, slices and maps, structs with methods, and goroutines with channels.

  1. 1. Package, import, main

    Every runnable Go program is package main with a main function. Unused imports and unused variables are compile errors on purpose — the language keeps files tidy for you.

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, Go")
    }
  2. 2. Declare with := and know your zero values

    Inside a function, := infers the type. Uninitialised values are never garbage: numbers are 0, strings are "", booleans are false, pointers and slices are nil.

    package main
    
    import "fmt"
    
    func main() {
        marks := 87
        var name string
        fmt.Printf("%q %d\n", name, marks)
    }
  3. 3. Return errors, do not throw them

    Go has no exceptions. A function that can fail returns (value, error) and the caller checks it immediately. This is the single most distinctive habit in Go.

    package main
    
    import (
        "errors"
        "fmt"
    )
    
    func divide(a, b float64) (float64, error) {
        if b == 0 {
            return 0, errors.New("division by zero")
        }
        return a / b, nil
    }
    
    func main() {
        v, err := divide(10, 0)
        if err != nil {
            fmt.Println("error:", err)
            return
        }
        fmt.Println(v)
    }
  4. 4. Slices, maps and structs with methods

    append grows a slice, a map needs make before use, and methods attach to a type through a receiver rather than living inside a class body.

    package main
    
    import "fmt"
    
    type Student struct {
        Name  string
        Marks int
    }
    
    func (s Student) Passed() bool { return s.Marks >= 50 }
    
    func main() {
        list := []Student{{"Aarav", 82}, {"Diya", 41}}
        for _, s := range list {
            fmt.Println(s.Name, s.Passed())
        }
    }
  5. 5. Concurrency with goroutines and channels

    go starts a lightweight thread; a channel passes values between goroutines and synchronises them. Receiving from a channel blocks until a value arrives.

    package main
    
    import "fmt"
    
    func main() {
        ch := make(chan string)
        go func() { ch <- "done" }()
        fmt.Println(<-ch)
    }

Practice exercises with solutions

Try each one in the editor above before opening the solution — the struggle is where the learning happens.

Beginner

Print the even numbers from 1 to 20 on one line.

Hint: for i := 1; i <= 20; i++ with an if on i%2.

Show solution
package main

import "fmt"

func main() {
    for i := 1; i <= 20; i++ {
        if i%2 == 0 {
            fmt.Print(i, " ")
        }
    }
    fmt.Println()
}
Intermediate

Count word frequencies in a sentence using a map.

Hint: strings.Fields splits on whitespace.

Show solution
package main

import (
    "fmt"
    "strings"
)

func main() {
    freq := map[string]int{}
    for _, w := range strings.Fields("code play code learn") {
        freq[w]++
    }
    fmt.Println(freq)
}
Advanced

Run three workers concurrently and collect their results in order.

Hint: A WaitGroup plus a pre-sized slice avoids locking.

Show solution
package main

import (
    "fmt"
    "sync"
)

func main() {
    results := make([]int, 3)
    var wg sync.WaitGroup
    for i := 0; i < 3; i++ {
        wg.Add(1)
        go func(i int) {
            defer wg.Done()
            results[i] = i * i
        }(i)
    }
    wg.Wait()
    fmt.Println(results)
}

Why learn Go?

Go compiles to a single binary with no runtime to install, which is why it dominates cloud infrastructure tooling like Docker and Kubernetes.

The language is deliberately small: most developers can read idiomatic Go within a weekend, and there is usually one obvious way to write something.

What Go is used for

  • Cloud infrastructure and DevOps tooling
  • High-throughput HTTP and gRPC services
  • Command-line utilities distributed as one binary
  • Concurrent data pipelines

Ready for structured practice? The 17-chapter Go course takes these ideas one at a time, with a quiz and a certificate at the end.