Go
Online Go Compiler
Code builds with go build from Go 1.27, and run settings switch to Go 1.26. fmt.Scan and bufio read what you type in the console while the program runs.
Try it aboveHow Go runs here
- Versions
- Go 1.26, 1.27 (default)
- Build
go build -o a.out main.go- Run
./a.out- Limits per run
- 512 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A program that reads input
One bufio.Reader reads a full line for the name, then numbers with fmt.Fscan:
package main import ( "bufio" "fmt" "os" "slices" "strings" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Your name: ") name, _ := reader.ReadString('\n') name = strings.TrimSpace(name) fmt.Print("How many numbers? ") var n int fmt.Fscan(reader, &n) nums := make([]int, n) fmt.Printf("Enter %d numbers: ", n) for i := range nums { fmt.Fscan(reader, &nums[i]) } slices.Sort(nums) fmt.Printf("%s, sorted: %v, max %d\n", name, nums, slices.Max(nums)) }
$ ./a.out Your name: Ada Lovelace How many numbers? 4 Enter 4 numbers: 8 3 10 5 Ada Lovelace, sorted: [3 5 8 10], max 10
Reading input in Go
- fmt.Scan(&a, &b) reads values separated by spaces or newlines. It’s the shortest way, and slow once input runs to around 10^5 values.
- bufio.NewReader(os.Stdin) with fmt.Fscan(reader, &x) reads the same way, much faster, and ReadString('\n') on the same reader gets whole lines.
- bufio.Scanner reads line by line with Scan() and Text(), but stops on a line over 64 KB with bufio.Scanner: token too long. Call scanner.Buffer(make([]byte, 1<<20), 1<<20) to allow longer lines.
- Use one reader for the whole program. A bufio reader takes more input than it hands back, so a later fmt.Scan can miss lines that were pasted together.
- For many output lines, write through w := bufio.NewWriter(os.Stdout) with defer w.Flush(). Leave out the Flush and nothing is printed.
The classic trap is fmt.Scanln with a string: it reads one word and reports an error if more follows on the line, and the rest of the line stays in the input for the next read:
package main import "fmt" func main() { var name string fmt.Print("Full name: ") _, err := fmt.Scanln(&name) fmt.Printf("name=%q err=%v\n", name, err) var age int fmt.Print("Age: ") _, err = fmt.Scan(&age) fmt.Printf("age=%d err=%v\n", age, err) }
$ ./a.out Full name: Ada Lovelace name="Ada" err=expected newline Age: age=0 err=expected integer
Read a line that may contain spaces with reader.ReadString('\n') and strings.TrimSpace, as in the first program.
One file per program
The build command names a single file, go build -o a.out main.go, so other .go files in the playground aren’t compiled with it. With a helper in helpers.go, main.go fails:
// helpers.go package main func double(x int) int { return x * 2 } // main.go package main import "fmt" func main() { fmt.Println(double(4)) }
$ go build -o a.out main.go # command-line-arguments ./main.go:6:14: undefined: double
- Keep every type and function of a program in main.go. A single file can be as long as you need.
- Run builds the open tab, so a second .go file with its own func main() is a separate program you can run from its tab.
- There’s no go.mod, so packages of your own in other folders can’t be imported either.
What isn’t available
- Third-party modules. There’s no network and no go.mod, so import "github.com/google/uuid" stops with no required module provides package github.com/google/uuid: go.mod file not found in current directory or any parent directory; see 'go help modules'. The golang.org/x packages aren’t part of the standard library either.
- cgo. CGO_ENABLED is 0, so a file that uses import "C" isn’t built.
- Parallel goroutines. Goroutines, channels and sync work, but GOMAXPROCS is 1, so they take turns on one core rather than running at the same time.
- Network access: net/http clients fail because runs are offline.
Errors you’re likely to see
- “"os" imported and not used”: Go refuses unused imports. Remove the import, or use it.
- “declared and not used: x”: unused local variables are errors too. Use the value, or assign it to _ while you work.
- “undefined: double”: the name doesn’t exist in main.go. It may be misspelled, or it’s defined in another file, which the build doesn’t see.
- “missing return”: a function with a result type can reach its end without a return statement.
- “panic: runtime error: index out of range [3] with length 3”: a slice index past the end. The run ends with Return Code 2 and a stack trace that names the line.
- “fatal error: all goroutines are asleep - deadlock!”: every goroutine is blocked, typically a send on an unbuffered channel with no receiver.
Compile errors start with ./main.go:6:14:, a link to that line. After a failed run, Fix with AI explains the error and proposes a change you review before it applies.
Go features from recent releases
Both offered versions have range over integers (Go 1.22), the min and max built-ins (Go 1.21), and iterators that slices and maps return (Go 1.23):
package main import ( "fmt" "maps" "slices" ) func main() { stock := map[string]int{"pears": 4, "apples": 7, "figs": 2} for _, name := range slices.Sorted(maps.Keys(stock)) { fmt.Printf("%-7s %d\n", name, stock[name]) } for i := range 3 { fmt.Print(i, " ") } fmt.Println(min(4, 9, 2), max(4, 9, 2)) }
$ ./a.out apples 7 figs 2 pears 4 0 1 2 2 9
- Map iteration order is random in Go, so slices.Sorted(maps.Keys(m)) is the short way to print a map in a stable order.
- Since Go 1.22 each loop iteration gets its own variable, so goroutines started in a for loop no longer all see the last value.
Questions
Which Go version is it?
Go 1.27 by default, and Go 1.26 from run settings.
Can I import packages from GitHub?
No. There’s no network and no go.mod, so only the standard library can be imported.
Why does fmt.Scanln read only the first word?
Scanln fills a string with one space-separated word and reports “expected newline” if more follows. Read the whole line with bufio.Reader’s ReadString('\n').
Can I split my program into several files?
Not here: the build compiles only the file being run, so keep the program in main.go.
Do goroutines work?
Yes. Goroutines and channels work as usual, on one core, so they interleave rather than run in parallel.
Why won’t Go compile with an unused variable?
Go treats unused imports and local variables as errors. Delete them, or write _ = x while the code is unfinished.