How to use the Go compiler
Write a program in the editor or pick one from Examples: input and arithmetic, slices, a prime check, strings, a word count with a map, recursion, structs and methods, interfaces, errors with panic and recover, and goroutines with channels. A program is package main with a func main. Press Run or Ctrl+Enter. fmt.Println and fmt.Printf write to the output; fmt.Fprintln(os.Stderr, …) writes in red.
Your code, input and tests are saved on this device as you type. Share makes a link with the code and input inside it, and Download saves the code as main.go.
How input works
The Input tab is the program's standard input. fmt.Scan(&a, &b) reads values in order, across spaces and lines; bufio.NewScanner(os.Stdin) reads it line by line, and bufio.NewReader(os.Stdin) with ReadString('\n') works too. When the input runs out, Scan reports an error and reads nothing, and the compiler notes that the program wanted more input.
What runs your code
Go 1.18, compiled in your browser by GopherJS: the Go compiler's own checks and messages (such as main.go:6:2: x declared but not used), then your program runs as JavaScript, with goroutines, channels, select, defer, panic and recover. Two differences from go run on a computer: generics are not supported, and int is 32 bits, as on a 32-bit computer, so it stops at 2147483647. Use int64 for bigger numbers. The standard library is there (fmt, strings, strconv, sort, math, time, errors, sync and more), but not files, networking or os/exec.
Each run may take 10 seconds; then it is stopped. Output is kept up to 10,000 lines or 1 MB, and the rest is cut with a notice. Programs cannot reach the network, and no packages or libraries can be installed: what a language brings with it is what you can use. A program that waits (time.Sleep, goroutines) is stopped at 10 seconds with what it printed; so is a loop that never ends.
Questions
- Which version of Go is this?
- Go 1.18, compiled to JavaScript by GopherJS in your browser. Generics are not supported.
- Why does a big number come out wrong?
- int is 32 bits here, so it wraps past 2147483647. Declare the variable as int64, which holds numbers up to about 9.2 × 10^18.
- Do goroutines and channels work?
- Yes: goroutines, buffered and unbuffered channels, select, sync.WaitGroup and sync.Mutex all work, and a deadlock is reported as Go reports it.
- Is there a step-by-step visualizer for Go?
- Not yet: Go runs on GopherJS here, which the visualizer cannot follow. Python, C, C++, Java, JavaScript and TypeScript have one.
See also
- Go Programming: Go from the first program to slices, maps, structs, interfaces, errors, goroutines and channels, with every program run in your browser.
- Practice problems, checked against hidden tests.
- Online Python compiler
- Online C compiler
- Online C++ compiler
- Online Java compiler
- Online SQL compiler
- Online JavaScript compiler
- Online TypeScript compiler
- Online 8086 Assembly compiler
- Online Lua compiler
- Online Ruby compiler
- Online PHP compiler