使用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?
我正在运行一个打开http端口的脚本。 我最初有记录错误: p>
log.Fatal(http.ListenAndServe(“:7777”,路由器))
code> pre>
\ n 我还有其他代码,所以我想在另一个线程上运行它: p>
转到http.ListenAndServe(“:7777”,路由器)
code> pre>
我仍然想记录与http端口失败有关的所有信息,但均无效: p>
go log.Fatal(http。 ListenAndServe(“:7777”,路由器))
code> pre>
Or p>
log.Fatal(go http.ListenAndServe (“:7777”,路由器))
code> pre>
正确的语法是什么? p>
div>
答
You can wrap it in an anonymous function:
go func() {
log.Fatal(http.ListenAndServe(":7777", router))
}()