How to compile a program in Go language?

I am taking the first steps with the Go language and I am 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, he says the following:

 # 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 them with 6g. But I try this way:

Compile this first Go program with: 6g test.go This compiles to a file: test.6 which is associated with the command: 6l test.6 This creates an executable file named: 6.out which is executed by the command: ./ 6.out and outputs the result: Hello, world

But nothing works, my code is:

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

So, I don’t know what else to do ... I now know the name of my compiler, so I have no idea how to compile it in Debian ... If you want, give it a hand ... I would really appreciate it!

+4
source share
2 answers

It looks like you are following the instructions:

The path to success: A thorough introduction to the Go Go programming language Ivo Balbaert. Section 2.3 Installation Go to Linux

These instructions are out of date. They use the legacy Go release, 0.60 release. You have installed Go version 1.1.1.

For the latest instructions, see Installing Go from the Source.

In addition, when you copy programs from a book, in the code examples, the book uses β€œ(left double quotation mark) andβ€œ (double quotation mark on the right). ”Go expectsβ€œ (quotation mark).

Write the test.go Go program as:

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

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

From the directory containing 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 result do you get?

+6
source

It looks like you have successfully installed Go from the source code, but you really need to work on a Go Tour , which will provide you with an introduction to Go programming concepts.

The code above is missing several sections. You need to import the "fmt" library, and then call any functions in it, previously using them with fmt. .

For instance:

 package main import "fmt" func main() { fmt.Println("Hello", "world") } 

I will also recommend in order the links to this page . They gradually introduce more complex concepts as they progress.

In addition, while using 6g is a valid way to compile Go code, it is more common to check the code with go run and compile using go build . See http://golang.org/cmd/go/ for more details.

I hope this helps.

+3
source

Source: https://habr.com/ru/post/1488400/


All Articles