Online Go Compiler
main.go
Ready
About Go
Go (Golang) is Google's simple, fast, compiled language, popular for back-end services, cloud tools and anything that handles many requests at once.
How to take input in Go
fmt.Scan reads values separated by spaces or new lines into the variables you pass by address.
var n int fmt.Scan(&n) var a, b int fmt.Scan(&a, &b)
Go programs to try
Copy one into the editor above, put any input in the Input tab and press Run.
Reverse a slice
package main
import "fmt"
func main() {
nums := []int{1, 2, 3, 4, 5}
for i, j := 0, len(nums)-1; i < j; i, j = i+1, j-1 {
nums[i], nums[j] = nums[j], nums[i]
}
fmt.Println(nums)
}Word count with a map
package main
import (
"fmt"
"strings"
)
func main() {
text := "go is fun and go is fast"
count := map[string]int{}
for _, w := range strings.Fields(text) {
count[w]++
}
fmt.Println(count["go"], count["is"])
}A function with two return values
package main
import "fmt"
func divide(a, b int) (int, int) {
return a / b, a % b
}
func main() {
q, r := divide(17, 5)
fmt.Println("quotient", q, "remainder", r)
}Common Go errors and how to fix them
- declared and not used
- Go refuses to compile if a variable is never used. Use it or remove it (or assign it to _).
- imported and not used
- Remove the import you're not using.
- panic: runtime error: index out of range
- A slice index is past its length. Check loop bounds and the slice's len().
Frequently asked questions
Is this online Go compiler free?
Yes. Write and run Go code in your browser for free, with nothing to install and no setup.
How do I give input to my Go program?
Type it in the Input tab before pressing Run and read it with fmt.Scan(&variable).
Why won't my Go code compile because of an unused variable?
Go treats unused variables and imports as errors. Remove them or use the blank identifier _.
Does it work on my phone?
Yes. The editor, input and output work in a mobile browser, so you can practise Go without a laptop.