如何在Go语言中编译程序?

huangapple go评论95阅读模式
英文:

How to compile a program in Go Language?

问题

我正在使用Go语言迈出第一步,并尝试在Debian Squeeze上安装它。
我按照下载源代码的步骤进行操作,然后在终端上执行了以下命令:

cd $GOROOT/src
./all.bash

最后,它显示如下信息:

# 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

所以,书上说我需要进行一些测试并使用6g进行编译。但是我尝试了以下方式:

使用以下命令首先编译Go程序:6g test.go
这将编译成一个文件:test.6
然后使用以下命令链接:6l test.6
这将生成可执行文件:6.out
使用以下命令执行可执行文件:./6.out
并输出:Hello, world

但是什么都不起作用,我的代码是:

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

所以,我不知道还应该做什么...我现在不知道我的编译器的名称,所以不知道如何在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!

答案1

得分: 14

看起来你正在按照以下指示操作:

> 《Go之道:Go编程语言的全面介绍》
> 作者:Ivo Balbaert
> 第2.3节 在Linux系统上安装Go

这些指示已经过时了。它们使用了一个过时的Go版本,即0.60版本。而你已经安装了Go的1.1.1版本。

要获取最新的安装指示,请参阅从源代码安装Go

此外,当你从书中复制程序时,书中的代码示例使用的是“(左双引号)和”(右双引号)。而Go语言使用的是"(引号)。

test.go的Go程序编写为:

package main

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

当你安装Go时,它告诉你“已在/root/go/bin中安装了命令”。你需要将/root/go/bin添加到你的$PATH中,以便它能够找到(识别)Go命令。

从包含test.go文件的目录中运行以下命令:

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

如果这个失败了,你得到了什么输出?
1: http://golang.org/doc/install/source

英文:

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?
1: http://golang.org/doc/install/source

答案2

得分: 3

看起来你已经成功地从源代码安装了Go,但是你应该通过Go Tour来逐步了解Go编程的概念。

你提供的代码缺少一些部分。你需要导入“fmt”库,并在调用其中的函数时在前面加上fmt.

例如:

package main

import "fmt"

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

我还建议按顺序阅读这个页面上的链接。它们会逐渐介绍更复杂的概念。

此外,虽然使用6g是编译Go代码的有效方法,但更常见的是使用go run来测试代码,并使用go build来编译。更多信息请参阅http://golang.org/cmd/go/。

希望对你有所帮助。

英文:

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.

答案3

得分: 0

这是官方下载网站:GO / Downloads

点击适合您系统的文件后,您将被重定向到包含安装过程说明的页面。

对于LinuxmacOSFreeBSD,特别是对于Debian tarballs,您应该将存档解压到 /usr/local(它将在 /usr/local/go 中创建一个Go目录,通常这些命令必须以root用户或通过sudo运行):

tar -C /usr/local -xzf go1.12.7.linux-amd64.tar.gz

并在您的用户的~/.profile文件末尾添加以下行:

PATH=$PATH:/usr/local/go/bin

对于当前终端会话,您也可以执行以下命令:

export PATH=$PATH:/usr/local/go/bin
英文:

Here is the official download site: GO / Downloads.

After clicking on the appropriate file for your system, you will redirect to the page with instructions on how to go through the installation process.

For Linux, macOS, and FreeBSD and in particular for Debian tarballs you should extract the archive into /usr/local (it will create a Go tree in /usr/local/go, typically these commands must be run as root or through sudo):

tar -C /usr/local -xzf go1.12.7.linux-amd64.tar.gz

and add this line at the end of the ~/.profile for your user:

PATH=$PATH:/usr/local/go/bin

For current terminal session you can also invoke this command:

export PATH=$PATH:/usr/local/go/bin

huangapple
  • 本文由 发表于 2013年6月27日 10:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/17333517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定