doumei1908
2013-06-27 02:19
浏览 48
已采纳

如何用Go语言编译程序?

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!

图片转代码服务由CSDN问答提供 功能建议

我正在使用Go语言迈出第一步,并试图将其安装在Debian Squeeze中。 我按照下载源代码的步骤进行操作,然后在终端上完成了该操作:

  cd $ GOROOT / src 
./all.bash 
   
 
 

最后说:

 #检查API兼容性。
Go版本为“ go1.1.1”, 忽略-next /root/go/api/next.txt
~pkg net,f​​unc ListenUnixgram(string,* UnixAddr)(* UDPConn,error)
〜pkg syscall(darwin-386),func Fchflags(string,int  )错误
〜pkg系统调用(darwin-386-cgo),func Fchflags(字符串,整数)错误
〜pkg系统调用(darwin-amd64),func Fchflags(字符串,整数)错误
〜pkg系统调用(darwin-  amd64-cgo),func Fchflags(string,int)错误
〜pkg syscall(freebsd-386),func Fchflags(string,int)error 
〜pkg syscall(freebsd-amd64),func Fchflags(string,int) 错误
〜pkg文本/模板/分析,键入DotNode bool 
〜pkg文本/模板/分析,键入Node接口{复制,字符串,类型} 
 
已通过所有测试
 
 --- 
 在/ root / go 
中找到linux / amd64,在/ root / go / bin 
   
 
 

中安装了命令 是的,我需要做一些测试并用6g进行编译。 但是我这样尝试:

使用以下命令编译第一个Go程序:6g test.go 它将编译为文件:test.6 ,该文件与命令链接:6l test .6 这将生成名为6.out 的可执行文件,该可执行文件将使用以下命令执行:./6.out ,并生成以下输出:Hello,world

但是没有任何效果,我的代码 是:

 包main 
func main(){
 println(“ H​​ello”,“ world”)
} 
   \  n 
 

所以,我不知道该怎么做...我现在知道我的编译器的名称,所以我不知道如何在Debian中进行编译...如果您愿意,请帮忙 有了这个……我真的很感谢你!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douxun4173 2013-06-27 06:28
    已采纳

    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?

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtw52353 2013-06-27 03:47

    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.

    评论
    解决 无用
    打赏 举报
  • dongxu4023 2019-08-05 10:41

    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
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题