使用go设置web服务器

使用go设置web服务器,go,Go,我不希望为我编写代码,我只是想在正确的方向上推动一下 我的任务是制作一个侦听端口8080的web服务器,在这个服务器上,我将呈现人类可读的数据。访问此服务器的人将使用/1、/2、/3等访问这些路径。要显示的数据将从5个不同的API收集,所有这些API都将以JSON格式返回数据 此外,所有路径都将使用Go模板呈现给此人 一个人怎么做呢? 我可能听起来像是在分发家庭作业,但我对这一点很陌生,需要一些帮助。我发现在学习做同样的事情时,以下几点非常有用: 这是一个很好的教程,介绍如何使用该软件包创建

我不希望为我编写代码,我只是想在正确的方向上推动一下

我的任务是制作一个侦听端口8080的web服务器,在这个服务器上,我将呈现人类可读的数据。访问此服务器的人将使用/1、/2、/3等访问这些路径。要显示的数据将从5个不同的API收集,所有这些API都将以JSON格式返回数据

此外,所有路径都将使用Go模板呈现给此人

一个人怎么做呢?
我可能听起来像是在分发家庭作业,但我对这一点很陌生,需要一些帮助。

我发现在学习做同样的事情时,以下几点非常有用:

  • 这是一个很好的教程,介绍如何使用该软件包创建一个简单的基于html的web应用程序。该包可用于从您使用的API收集信息,以及发送您的响应json。它引入了html模板,但json模板的过程基本上没有改变

  • Go的模板引擎概述

  • 一篇关于在json中使用go编码(封送)和解码(解组)数据的博客文章


我发现在学习做同样的事情时,以下几点非常有用:

  • 这是一个很好的教程,介绍如何使用该软件包创建一个简单的基于html的web应用程序。该包可用于从您使用的API收集信息,以及发送您的响应json。它引入了html模板,但json模板的过程基本上没有改变

  • Go的模板引擎概述

  • 一篇关于在json中使用go编码(封送)和解码(解组)数据的博客文章


    • 您将从答案中获得大量资源。我想给出一些简单的代码,您可以测试它是否符合您的需要:

      具有如下简单的文件夹结构:

      ProjectName
      ├── main.go
      └── templates
          └── index.html
      
      main.go中
      我们创建了一个http服务器,监听端口8080。以下是注释的完整代码:

      main.go

      package main
      
      import (
          "encoding/json"
          "fmt"
          "html/template"
          "io/ioutil"
          "net/http"
      )
      
      // User information of a GitHub user. This is the
      // structure of the JSON data you are rendering so you
      // customize or make other structs that are inline with
      // the API responses for the data you are displaying
      type User struct {
          Name     string `json:"name"`
          Company  string `json:"company"`
          Location string `json:"location"`
          Email    string `json:"email"`
      }
      
      func main() {
          templates := template.Must(template.ParseFiles("templates/index.html"))
      
          // The endpoint
          http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
              user, err := getGithubUser("musale")
              if err != nil {
                  http.Error(w, err.Error(), http.StatusInternalServerError)
              }
              if err := templates.ExecuteTemplate(w, "index.html", user); err != nil {
                  http.Error(w, err.Error(), http.StatusInternalServerError)
              }
          })
      
          // Start the server on 8080
          fmt.Println(http.ListenAndServe(":8080", nil))
      }
      
      // One of your API endpoints
      func getGithubUser(username string) (User, error) {
          var resp *http.Response
          var err error
          var user User
          // The endpoint
          const githubUserAPI = "https://api.github.com/users/"
      
          // Get the required data in json
          if resp, err = http.Get(githubUserAPI + username); err != nil {
              return user, err
          }
      
          defer resp.Body.Close()
      
          var body []byte
          if body, err = ioutil.ReadAll(resp.Body); err != nil {
              return user, err
          }
      
          // Unmarshal the response into the struct
          if err = json.Unmarshal(body, &user); err != nil {
              return user, err
          }
      
          return user, nil
      }
      
      然后在
      index.html
      中使用:

      <!DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Github User</title>
          <meta name="viewport" content="width=device-width, initial-scale=1">
      </head>
      
      <body>
          <p>Name: {{.Name}}</p>
          <p>Company: {{.Company}}</p>
          <p>Location: {{.Location}}</p>
          <p>Email: {{.Email}}</p>
      </body>
      </html>
      
      
      Github用户
      名称:{{.Name}

      公司:{{.Company}

      位置:{{.Location}

      电子邮件:{{.Email}


      大部分资源都是针对代码片段的,通过进一步的修改,您将能够将参数传递到URL,根据路由呈现数据等。我希望这能让您了解如何轻松解决问题。祝你好运

      您将从答案中获得大量资源。我想给出一些简单的代码,您可以测试它是否符合您的需要:

      具有如下简单的文件夹结构:

      ProjectName
      ├── main.go
      └── templates
          └── index.html
      
      main.go中
      我们创建了一个http服务器,监听端口8080。以下是注释的完整代码:

      main.go

      package main
      
      import (
          "encoding/json"
          "fmt"
          "html/template"
          "io/ioutil"
          "net/http"
      )
      
      // User information of a GitHub user. This is the
      // structure of the JSON data you are rendering so you
      // customize or make other structs that are inline with
      // the API responses for the data you are displaying
      type User struct {
          Name     string `json:"name"`
          Company  string `json:"company"`
          Location string `json:"location"`
          Email    string `json:"email"`
      }
      
      func main() {
          templates := template.Must(template.ParseFiles("templates/index.html"))
      
          // The endpoint
          http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
              user, err := getGithubUser("musale")
              if err != nil {
                  http.Error(w, err.Error(), http.StatusInternalServerError)
              }
              if err := templates.ExecuteTemplate(w, "index.html", user); err != nil {
                  http.Error(w, err.Error(), http.StatusInternalServerError)
              }
          })
      
          // Start the server on 8080
          fmt.Println(http.ListenAndServe(":8080", nil))
      }
      
      // One of your API endpoints
      func getGithubUser(username string) (User, error) {
          var resp *http.Response
          var err error
          var user User
          // The endpoint
          const githubUserAPI = "https://api.github.com/users/"
      
          // Get the required data in json
          if resp, err = http.Get(githubUserAPI + username); err != nil {
              return user, err
          }
      
          defer resp.Body.Close()
      
          var body []byte
          if body, err = ioutil.ReadAll(resp.Body); err != nil {
              return user, err
          }
      
          // Unmarshal the response into the struct
          if err = json.Unmarshal(body, &user); err != nil {
              return user, err
          }
      
          return user, nil
      }
      
      然后在
      index.html
      中使用:

      <!DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Github User</title>
          <meta name="viewport" content="width=device-width, initial-scale=1">
      </head>
      
      <body>
          <p>Name: {{.Name}}</p>
          <p>Company: {{.Company}}</p>
          <p>Location: {{.Location}}</p>
          <p>Email: {{.Email}}</p>
      </body>
      </html>
      
      
      Github用户
      名称:{{.Name}

      公司:{{.Company}

      位置:{{.Location}

      电子邮件:{{.Email}


      大部分资源都是针对代码片段的,通过进一步的修改,您将能够将参数传递到URL,根据路由呈现数据等。我希望这能让您了解如何轻松解决问题。祝你好运

      这对你来说应该是一个好的和广泛的介绍:这对你来说应该是一个好的和广泛的介绍: