Lui*_*ado 5 golang software-installation
如何在 Ubuntu 中正确安装和配置 Go 语言。有很多软件包可供选择,但是我需要安装哪些软件包以及之后我需要配置什么才能使用任何 Go 软件包而不会出现“找不到软件包”错误或任何其他基本错误那样。
我安装了该golang软件包,但是否需要安装任何其他软件包或配置其他内容?
作为示例,尝试运行以下命令:
package main
import (
"http"
"log"
)
func HelloServer(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Connection", "keep-alive")
w.Write([]byte("hello, world!\n"))
}
func main() {
http.HandleFunc("/", HelloServer)
log.Println("Serving at http://127.0.0.1:8080/")
http.ListenAndServe(":8080", nil)
}
Run Code Online (Sandbox Code Playgroud)
安装golang元包应该就足够了:
sudo apt-get install golang
Run Code Online (Sandbox Code Playgroud)
“这个包是一个元包,安装后可以保证安装(大部分)完整的 Go 开发环境。” 因此,之后您只需要输入go help基本命令:
Go is a tool for managing Go source code.
Usage:
go command [arguments]
The commands are:
build compile packages and dependencies
clean remove object files
env print Go environment information
fix run go tool fix on packages
fmt run gofmt on package sources
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet run go tool vet on packages
Run Code Online (Sandbox Code Playgroud)
在 gedit 中创建一个 hello world。来自他们网站的示例:
package main
import "fmt"
func main() {
fmt.Println("Hello world\n")
}
Run Code Online (Sandbox Code Playgroud)
(另存为hello.go)
执行...
go run hello.go
Run Code Online (Sandbox Code Playgroud)
产生...
Hello world
Run Code Online (Sandbox Code Playgroud)
gorun让你使用she-bang。不过,请阅读此主题。上面的例子可以是:
#!/usr/bin/gorun
package main
func main() {
println("Hello world!\n")
}
Run Code Online (Sandbox Code Playgroud)
并使其可执行:
chmod +x hello.go
./hello.go
Run Code Online (Sandbox Code Playgroud)
产生...
Hello world!
Run Code Online (Sandbox Code Playgroud)
(我自己加了\n)
你的例子有一个错误:
进口http需要net/http
go run test.go
2014/05/10 20:15:00 Serving at http://127.0.0.1:8080/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2568 次 |
| 最近记录: |