I have installed Go programming language (golang) on my Raspberry Pi system becky a while ago. It's time to continue experiments, and we'll start with the simplest.
Hello, World Example in Go
Create a file named hello.go (or anything else with .go extension):
greys@becky:~ $ vim hello.go
and put the following in it:
package main func main() { print("hello, world\n") }
That's it! Save the file and let's try running it.
I din't know, but Go natively supports two ways of executing a program: interptet it line by line or build the executable binary from Go source code.
Running a Go Program
Here's how we run the go program: just specify the run command line option and the name to our hello.go program:
greys@becky:~ $ go run hello.go hello, world
Building a Go Program
If you want to create a binary file first, let's build the binary using build command line option:
greys@becky:~ $ go build hello.go
If you see no error messages, this is a good sign. Check for binary file in the same directory:
greys@becky:~ $ ls -la hello*
-rwxr-xr-x 1 greys tty 871022 Aug 8 23:30 hello
-rw-r--r-- 1 greys tty 56 Aug 8 23:23 hello.go
Because I'm using ls with colour output, I can immediately see that it's an executable file (I can see the same from x bit in the permissions as well):
Let's run the binary, and we'll get the same result:
greys@becky:~ $ ./hello hello, world
That's it for today! Have fun!
Leave a Reply