Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法将golang net/http应用程序公开到我的本地网络?_Go_Networking - Fatal编程技术网

有没有办法将golang net/http应用程序公开到我的本地网络?

有没有办法将golang net/http应用程序公开到我的本地网络?,go,networking,Go,Networking,我目前正在开发一个golang/ginAPI和一个React前端,我正在尝试在手机上查看它 React自然会在我的本地网络上公开应用程序,如下所示: You can now view dashboard in the browser. Local: http://localhost:3000 On Your Network: http://xxx.xxx.x.xx:3000 但是,ginAPI只能从我的本地计算机上访问:它不会暴露在我的本地网络上 这能实现吗?

我目前正在开发一个
golang/gin
API和一个
React
前端,我正在尝试在手机上查看它

React自然会在我的本地网络上公开应用程序,如下所示:

You can now view dashboard in the browser.

  Local:            http://localhost:3000
  On Your Network:  http://xxx.xxx.x.xx:3000
但是,
gin
API只能从我的本地计算机上访问:它不会暴露在我的本地网络上

这能实现吗?如何实现

编辑1:我确实在网上搜索过答案,但没有成功


编辑2:找到的解决方案与网络曝光无关,而是前端的打字错误

当然。从包含以下内容的包运行web服务器:

http.ListenAndServe(":8080", nil)
如果您必须使用杜松子酒,您可以使用他们家中的示例:


我犯了一个愚蠢的错误:


在前端,当我联系API时,我正在获取
http://localhost:8080
而不是
http://xxx.xxx.x.xx:8080

也许我想在我的帖子里说得不够清楚,对此我深表歉意。我在我的
localhost
上运行web服务器,但在网络上无法访问它。虽然通过写下这条评论,我刚刚找到了问题的答案。@DiegoROJAS这个答案描述了如何监听所有接口。如果此答案中的代码不适用于您,则问题在于防火墙或Go程序范围之外的其他问题。这不是问题的答案。Kenny Grant为您的问题编写了正确的答案。
package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}