Go Playground
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:
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:
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:
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:
- Execution time limit: Each code execution has a time limit, usually a few seconds
- Memory limit: Memory usage is limited
- Network access: Cannot access external network resources
- File system access: Cannot access local file system
- 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:
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
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}Concurrency Example
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
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 codeCtrl + Shift + F/Cmd + Shift + F: Format codeCtrl + 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:
- Install Go locally: Visit Install Go to install Go on your computer
- Learn Go basics: Visit Go Getting Started Guide
- Read official documentation: Visit Go Official Documentation
- 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!
