所以我目前正在使用 gin-gonic 包在 go 中构建一个 Restful api。我希望将代码部署到谷歌云平台计算引擎虚拟机上。当我在本地计算机上运行代码时,它可以使用本地主机,但当在指定外部 IP 的实际 VM 实例上运行它时,我收到 TCP 连接绑定错误。任何帮助表示赞赏。
服务器.go
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
type headlines struct {
Author string
Title string
Description string
Url string
UrlToImage string
PublishedAt string
Content string
}
type NewsResponse struct {
Status string
TotalResults int
Articles []headlines
}
func GetSourceHeadlines(source string) NewsResponse {
newsAPIKey := os.Getenv("NEWS_API_KEY")
var newsResponse NewsResponse
resp, err := http.Get("https://newsapi.org/v2/top-headlines?sources=" + source + "&apiKey=" + newsAPIKey)
if err != nil { …Run Code Online (Sandbox Code Playgroud)