Compiling Go Program

First install the go language and compiler:

$ sudo pkg install go-1.5.1,1
$ go version
go version go1.5.1 freebsd/386

Then create a file called pe_prob001.1.go:

package main

import "fmt"

func main() {

    var sum int
    sum = 0

    for i := 1; i < 1000; i++ {
        if i % 3 == 0 || i % 5 == 0 {
            sum += i
        }
    }

    fmt.Println("sum is", sum)
}

To build an executable, run the command

$ go build pe_prob001.1.go

To run the executable, type:

$ ./pe_prob001.1

To just run the program directly, you can run the command:

$ go run pe_prob001.1.go
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License