douhui1333
douhui1333
2018-01-09 21:33

使用go命令进行log.Fatal操作

I'm running a script which opens an http port. I initially had it logging errors:

log.Fatal(http.ListenAndServe(":7777", router))

I have additional code so I want to run it on another thread:

go http.ListenAndServe(":7777", router)

I still want to log any information related to the http port failing, but neither work:

go log.Fatal(http.ListenAndServe(":7777", router))

Or

log.Fatal(go http.ListenAndServe(":7777", router))

What is the correct syntax for this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongliang1654 dongliang1654 3年前

    You can wrap it in an anonymous function:

    go func() {
        log.Fatal(http.ListenAndServe(":7777", router))
    }()
    
    点赞 评论 复制链接分享

为你推荐