Go Language Practice¶
GO QUEST
Explore Go code
Practice Go fundamentals with single-line code exercises.
sandbox / go
Enter one line of code and press Enter to see simulated output.
Complete code examples
The examples below are for reading and copying; they cannot be run in the simulated terminal above. Each is a standalone Go program that can be saved as main.go and run with go run main.go.
Fibonacci sequence: loops and functions
package main
import "fmt"
func fibonacci(n int) []int {
result := make([]int, 0, n)
a, b := 0, 1
for i := 0; i < n; i++ {
result = append(result, a)
a, b = b, a+b
}
return result
}
func main() {
fmt.Println(fibonacci(7))
}
Output: [0 1 1 2 3 5 8]
Parse JSON: structs and error handling
package main
import (
"encoding/json"
"fmt"
)
type Service struct {
Name string `json:"name"`
Port int `json:"port"`
}
func main() {
data := []byte(`{"name":"web","port":8080}`)
var service Service
if err := json.Unmarshal(data, &service); err != nil {
fmt.Println("Parse failed:", err)
return
}
fmt.Printf("%s: %d\n", service.Name, service.Port)
}
Output: web: 8080
Goroutines and channels
package main
import "fmt"
func main() {
result := make(chan int)
go func() {
result <- 6 * 7
}()
fmt.Println(<-result)
}
Output: 42
The Workbench pipeline agent provides a Go build environment.