doumu5023 2016-07-25 13:42
浏览 29
已采纳

使用内置依赖项进行构建

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 (
    "fmt"
    "log"
    "os/exec"
    "net/http"
)

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.

  • 写回答

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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog