2016-07-25 13:42
浏览 21


I'm very new to system programming.

I have a Go program which uses net/http and starts an http server.

When I build a Windows binary and tried on a target Windows machine, nothing worked except Printfs before it starts the server.

As soon as I installed Go on the target Windows machine, everything started working!

Here is my program:

package main

import (

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there @%s!", r.URL.Path[1:])

func main() {

    path, err := exec.LookPath("go")
    if err != nil {
        log.Fatal("Go is not your fortune :|")
    fmt.Printf("Go is available at %s
", path)

    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)

If go doesn't build all the dependencies with its program then how do I do it? If it does, why it is not working?

Does target systems have to have go installed prior to run any Go programs?

Please help! Thanks.

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


我有一个Go语言 使用 net / http 并启动http服务器的程序。

当我构建Windows二进制文件并在目标Windows计算机上尝试时,除 在启动服务器之前,先执行Printf s。



“ fmt” 
“ log” 
“ os / exec” 
“ net / http”  
func处理程序(w http.ResponseWriter,r * http.Request){
 fmt.Fprintf(w,“ Hi there @%s!”,r.URL.Path [1:])
func main(){
 path,err:= exec.LookPath(“ go”)
 if err!= nil {
 log.Fatal(“ Go不是你的财富:|”)  
 fmt.Printf(“ Go在%s 
 http.HandleFunc(“ /”,处理程序)
 http.ListenAndServe(“:8080”,nil)\  n} 

如果 go 不能构建所有依赖项 然后用它的程序怎么做? 如果可以,为什么它不起作用?

目标系统是否必须先安装 go 才能运行任何Go程序?

请帮助! 谢谢。

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

2条回答 默认 最新

  • drkjzk3359 2016-07-25 14:17

    Your app doesn't work without Go because you call log.Fatal() if it doesn't find the go tool, and log.Fatal() terminates your app:

    Fatal is equivalent to Print() followed by a call to os.Exit(1).

    Your executable binary will contain everything it needs if you build it from the source you posted either with go build or go install (see What does go build build? for details). Just don't call log.Fatal() and it should work.

    And on a side note: you should check and print errors returned by http.ListenAndServe(), e.g.:

    panic(http.ListenAndServe(":8080", nil))

    Because if this fails, you won't know why it doesn't work (e.g. you already started the app and the port is taken / in use).

    解决 无用
    打赏 举报

相关推荐 更多相似问题