Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a program in Go Language?

Tags:

go

debian

I'm walking the first steps with Go language and I'm trying to install it in Debian Squeeze. I follow the step of downloading the source code and then, I did this on my terminal:

cd $GOROOT/src
./all.bash

At the end, it says this:

# Checking API compatibility.
Go version is "go1.1.1", ignoring -next /root/go/api/next.txt
~pkg net, func ListenUnixgram(string, *UnixAddr) (*UDPConn, error)
~pkg syscall (darwin-386), func Fchflags(string, int) error
~pkg syscall (darwin-386-cgo), func Fchflags(string, int) error
~pkg syscall (darwin-amd64), func Fchflags(string, int) error
~pkg syscall (darwin-amd64-cgo), func Fchflags(string, int) error
~pkg syscall (freebsd-386), func Fchflags(string, int) error
~pkg syscall (freebsd-amd64), func Fchflags(string, int) error
~pkg text/template/parse, type DotNode bool
~pkg text/template/parse, type Node interface { Copy, String, Type }

    ALL TESTS PASSED

---
Installed Go for linux/amd64 in /root/go
Installed commands in /root/go/bin

So, the book says that I need to do some tests and compile it with 6g. But I try it this way:

Compile this first Go-program with: 6g test.go This compiles to a file: test.6 which is linked with the command: 6l test.6 This produces the executable named: 6.out which executes with the command: ./6.out and produces the output: Hello, world

But nothing works, my code is:

package main
func main() {
         println(“Hello”, “world”)
}

So, I do not know what more to do... I do know now the name of my compiler, so I have no idea how to compile this in Debian... If you please, give a hand with this... I would be really thankfully to you!

like image 419
Javittoxs Avatar asked Jun 27 '13 02:06

Javittoxs


People also ask

Does Go run compile?

The go run command compiles and runs a main package comprised of the .go files specified on the command line. The command is compiled to a temporary folder. The go build and go install examine the files in the directory to determine which .go files are included in the main package.

Does golang have a compiler?

Golang is a compiler-based language, it can easily be compiled on the development computer for any targeted system such as linux and mac. A golang project when have compiled turns to a self-sufficient executable and can be ran on the targeted system without anything additional.

How do I run a program in golang?

To run a go program, create a file with an extension .go and write your golang code in that file. For example, we will create a file named helloworld.go and write the following code in it. Now open command prompt and navigate to the location of helloworld.go file. Run the following command.

Is Go fully compiled?

Go programs, like C and C++ programs, are fully-compiled so they require no run-time engine and no run-time translation. In practical terms, this means that Go programs will typically run much faster than Python or JavaScript programs, especially if your program makes heavy use of the system CPU or RAM.


2 Answers

It looks like you are following instructions from:

The Way to Go: A Thorough Introduction to the Go Programming Language By Ivo Balbaert. Section 2.3 Installing Go on a Linux system

These instructions are out of date. They use an obsolete release of Go, release 0.60. You have installed Go release 1.1.1.

For up-to-date instructions see Installing Go from source

Also, when you copy programs from the book, the book uses “ (left double quotation mark) and ” (right double quotation mark) in the code examples. Go expects " (quotation mark).

Write the test.go Go program as:

package main

func main() {
    println("Hello", "world")
}

When you installed Go, it told you it "Installed commands in /root/go/bin." You need to have /root/go/bin in your $PATH so that it can find (recognize) the Go commands.

From the directory which contains the test.go file, run

$ export PATH=$PATH:/root/go/bin
$ go version
go version go1.1.1 linux/amd64
$ go run test.go
Hello world

If this fails, what output do you get?

like image 131
peterSO Avatar answered Oct 13 '22 01:10

peterSO


It looks like you've successfully installed Go from source, but you should really work your way through the Go Tour which will provide an introduction to the concepts of programming in Go.

The code you provided is missing a few sections. You need to import the "fmt" library, and then call any functions in it by prefacing them with fmt. .

For example:

package main

import "fmt"

func main() {
         fmt.Println(“Hello”, “world”)
}

I'd also recommend going through the links on this page in order. They gradually introduce more complex concepts as they go along.

Also, although using 6g is a valid way of compiling Go code, it's more usual to test code using go run, and to compile using go build. See http://golang.org/cmd/go/ for more info.

I hope that helps.

like image 26
Intermernet Avatar answered Oct 13 '22 01:10

Intermernet