Skip to content

Go Playground ​

Before You Start

  • Applies to: Go 1.21+ (examples verified with Go 1.22) · Prerequisites: Get Started · Read time: about 6 min

Go Playground is an online tool provided by Go official website that allows you to write, compile and run Go code without installing Go locally.

What is Go Playground? ​

Go Playground is a web-based Go code editor and execution environment with the following features:

  • No installation required: Run Go code directly in the browser
  • Real-time compilation: Quickly compile and run code
  • Code sharing: Generate shareable links to share code with others
  • Multiple versions: Support different Go language versions
  • Standard library support: Can use most of Go's standard library

Access Go Playground ​

Visit the official Go Playground: https://go.dev/play/

Basic Usage ​

1. Write Code ​

In the code editor area, you can write or paste Go code. The default is a simple Hello World program:

go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

2. Run Code ​

Click the "Run" button or press Ctrl + Enter (Windows/Linux) / Cmd + Enter (macOS) to execute the code.

3. View Results ​

The output will be displayed in the output area at the bottom.

4. Share Code ​

Click the "Share" button to generate a shareable link. You can send this link to others to let them view and run your code.

Advanced Features ​

Format Code ​

Click the "Format" button or press Ctrl + Shift + F to automatically format the code to comply with Go's code style specifications.

Import Packages ​

Go Playground supports most standard library packages. You can import packages as needed:

go
package main

import (
    "fmt"
    "math"
    "strings"
)

func main() {
    fmt.Println(math.Pi)
    fmt.Println(strings.ToUpper("hello"))
}

Use Third-Party Packages ​

Go Playground supports some third-party packages. You can use import statements to import them:

go
package main

import (
    "fmt"
    "github.com/gorilla/mux"
)

Set Go Version ​

You can choose different Go language versions in the "Go version" dropdown menu. This is useful for testing code compatibility.

Limitations ​

Go Playground has some limitations:

  1. Execution time limit: Each code execution has a time limit, usually a few seconds
  2. Memory limit: Memory usage is limited
  3. Network access: Cannot access external network resources
  4. File system access: Cannot access local file system
  5. CPU time limit: CPU usage time is limited

Best Practices ​

1. Test Code Snippets ​

Go Playground is ideal for testing small code snippets and algorithms:

go
package main

import "fmt"

func main() {
    // Test sorting algorithm
    numbers := []int{5, 2, 8, 1, 9}
    
    // Simple bubble sort
    for i := 0; i < len(numbers)-1; i++ {
        for j := 0; j < len(numbers)-i-1; j++ {
            if numbers[j] > numbers[j+1] {
                numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
            }
        }
    }
    
    fmt.Println(numbers)
}

2. Demonstrate Code ​

Use Go Playground to demonstrate code to others. Generate shareable links to let others view and run your code.

3. Learn Go Language ​

For beginners, Go Playground is a good learning tool. You can try different code examples without installing Go locally.

4. Debug Code ​

Go Playground can help you debug code. You can quickly test code fragments to find problems.

Common Examples ​

Hello World ​

go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Concurrency Example ​

go
package main

import (
    "fmt"
    "time"
)

func worker(id int, jobs <-chan int, results chan<- int) {
    for j := range jobs {
        fmt.Printf("Worker %d started job %d\n", id, j)
        time.Sleep(time.Second)
        results <- j * 2
        fmt.Printf("Worker %d finished job %d\n", id, j)
    }
}

func main() {
    jobs := make(chan int, 100)
    results := make(chan int, 100)
    
    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }
    
    for j := 1; j <= 5; j++ {
        jobs <- j
    }
    close(jobs)
    
    for a := 1; a <= 5; a++ {
        <-results
    }
}

Struct Example ​

go
package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func (p Person) String() string {
    return fmt.Sprintf("%s (%d years old)", p.Name, p.Age)
}

func main() {
    person := Person{
        Name: "Alice",
        Age:  30,
    }
    fmt.Println(person)
}

Tips and Tricks ​

Keyboard Shortcuts ​

  • Ctrl + Enter / Cmd + Enter: Run code
  • Ctrl + Shift + F / Cmd + Shift + F: Format code
  • Ctrl + S / Cmd + S: Save code

Code Formatting ​

Go Playground automatically formats code. It is recommended to always keep code formatting consistent.

Error Handling ​

If code has errors, error messages will be displayed in the output area. Please carefully read error messages to locate problems.

Alternative Tools ​

Besides Go Playground, there are other online Go code execution environments:

Next Steps ​

After getting familiar with Go Playground, you can:

  1. Install Go locally: Visit Install Go to install Go on your computer
  2. Learn Go basics: Visit Go Getting Started Guide
  3. Read official documentation: Visit Go Official Documentation
  4. Join the community: Participate in the Go community to get help and share experience

Conclusion ​

Go Playground is a powerful and convenient tool suitable for quickly testing code, learning Go language, and demonstrating code to others. Although it has some limitations, it is still an indispensable tool for Go developers.

Whether you are a beginner or an experienced developer, Go Playground can provide you with convenience. Start using Go Playground to experience the charm of Go language!

Complete Example ​

Paste the program below into the Go Playground and press Run. It also runs locally with go run main.go.

go
package main

import "fmt"

func main() {
	for i := 1; i <= 15; i++ {
		switch {
		case i%15 == 0:
			fmt.Println("FizzBuzz")
		case i%3 == 0:
			fmt.Println("Fizz")
		case i%5 == 0:
			fmt.Println("Buzz")
		default:
			fmt.Println(i)
		}
	}
}

Expected output:

text
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Common Pitfalls ​

  • The Playground has no network access. Programs that call external APIs or download modules will not run there.
  • Deterministic output only. The Playground caches results, so time.Now(), random numbers and map iteration order can surprise you.
  • Long-running programs are killed. Keep experiments short; the sandbox has a strict time limit.
  • Go version may differ from your machine. Check the version selector to match the language features you use.
  • Share, don't paste binaries. Use the share URL for code; the Playground is for source, not artifacts.

Change Log ​

  • 2026-10-14: Added "Before You Start" notes, a complete runnable example with expected output, common pitfalls, and this change log.

Golang by www.golangdev.cn · Adapted in part from Golang 中文学习文档 (MIT, © 寒江蓑笠翁)