Hi there! A couple days ago I wrote a post about a simple array sum in Go and I said I didn’t need an array in memory to do the sum, but just an integer that would store the inputs the user gave the program. This time, I will really need an array.
The goal is to read an array $A$ of $N$ elements and print $A$ elements in reverse order. Surely I could put everything in a file and then read it backwards, but as I don’t even know how to use files in Go at the moment, I will be creating an array in memory and then printing it backwards to the screen. My solution is just below.
package main
import (
"fmt"
)
func main() {
var N int
fmt.Scan(&N)
var a = make([]int, N)
for j := 0; j < N; j++ {
fmt.Scan(&a[j])
}
for i := len(a) - 1; i >= 0; i-- {
fmt.Printf("%d ", a[i])
}
fmt.Printf("\n")
}
The main point that was different for me was the way an array is defined. You
use []int
instead of int[]
like in C
. I am not sure what was the purpose
of that, maybe just to be different from C
haha.
That’s all for today, thanks for reading! If you have any questions, complaints or suggestions, please comment!