duankanyi6539 2018-10-22 21:47
浏览 15
已采纳

将字符串连接到结构片段

I'm trying to create an endpoint Go API to be consumed by front end JavaScript graphing library.

What should happen:

  1. /data endpoint to get an array of JSON data.
  2. The data is just a slice of go structs, just two in this example but it will be over 10000.
  3. The graphing library expects additional meta data. It's saved as a variable called AdditionalInfo.
  4. The problem is when I want to encode the AdditionalInfo + AllAgents it won't let me join a string with a slice of structs.
  5. At the end of the slice of structs there also needs to be ]` to make it valid JSON

Problems:

  1. How do you add additional data at the front and back the slice of structs?
  2. Is appending a struct to a slice of structs efficient?
  3. Would it be better to create a slice with a known length and then use range to append to it?

    package main
    
    import (
        "encoding/json"
        "fmt"
        "log"
        "net/http"
        "os"
    )
    
    func main() {
        port := os.Getenv("PORT")
        if port == "" {
            port = "443"
            log.Printf("Defaulting to port %s", port)
        }
    
        http.HandleFunc("/data", TrySnipped)
        log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
    
    }
    
    func TrySnipped(w http.ResponseWriter, r *http.Request) {
        w.Header().Add("Content-Type", "application/json")
        var AllAgents []SingleAgent
        AllAgents = append(AllAgents, FirstAgent)
        AllAgents = append(AllAgents, SecondAgent)
    
        json.NewEncoder(w).Encode(AllAgents)
    
    }
    
    var AdditionalInfo = `"meta": {
                "page": 1,
                "pages": 1,
                "perpage": -1,
                "total": 350,
                "sort": "asc",
                "field": "RecordID"
            },
            "data": [`
    
    type SingleAgent struct {
        RecordID             string `json:"RecordID"`
        OldEmployeeID        string `json:"OldEmployeeID"`
        NewEmployeeID        string `json:"NewEmployeeID"`
        AgentRank            int    `json:"AgentRank"`
        AgentName            string `json:"AgentName"`
        AgentEmail           string `json:"AgentEmail"`
        TeamManager          string `json:"TeamManager"`
        LineOfBusiness       string `json:"LineOfBusiness"`
        FitnessCampStartDate string `json:"FitnessCampStartDate"`
        FitnessCampEndDate   string `json:"FitnessCampEndDate"`
        FitnessCampStatus    int    `json:"FitnessCampStatus"`
        AgentFitnessRank     int    `json:"AgentFitnessRank"`
        LatestMetricA        int    `json:"MetricA"`
        LatestMetricB        int    `json:"MetricB"`
        LatestMetricC        int    `json:"MetricC"`
        LatestMetricD        int    `json:"MetricD"`
        LatestMetricE        int    `json:"MetricD"`
        SiteName             string `json:"SiteName"`
        SiteID               string `json:"SiteID"`
        PostCampWeek1        string `json:"PostCampWeek1"`
        PostCampWeek2        string `json:"PostCampWeek2"`
        PostCampWeek3        string `json:"PostCampWeek3"`
    }
    
    var FirstAgent = SingleAgent{
        RecordID:             "30",
        OldEmployeeID:        "785114544",
        NewEmployeeID:        "10785114544",
        AgentRank:            3,
        AgentName:            "Shandeigh Rawsen",
        AgentEmail:           "shandeigh.rawsen@gmail.com",
        TeamManager:          "Oliver Lamplugh",
        LineOfBusiness:       "BMS",
        FitnessCampStartDate: "8/13/2018",
        FitnessCampEndDate:   "10/7/2018",
        FitnessCampStatus:    1,
        AgentFitnessRank:     1,
        LatestMetricA:        77,
        LatestMetricB:        167,
        LatestMetricC:        496,
        LatestMetricD:        72,
        LatestMetricE:        80,
        SiteName:             "Montreal",
        SiteID:               "4",
        PostCampWeek1:        "10/2/2018",
        PostCampWeek2:        "10/9/2018",
        PostCampWeek3:        "10/19/2018",
    }
    var SecondAgent = SingleAgent{
        RecordID:             "29",
        OldEmployeeID:        "980433290",
        NewEmployeeID:        "10980433290",
        AgentRank:            4,
        AgentName:            "Terrijo Dewberry",
        AgentEmail:           "terrijo.deberry@gmail.com",
        TeamManager:          "Oliver Lamplugh",
        LineOfBusiness:       "BTSW",
        FitnessCampStartDate: "7/28/2018",
        FitnessCampEndDate:   "9/23/2018",
        FitnessCampStatus:    2,
        AgentFitnessRank:     2,
        LatestMetricA:        88,
        LatestMetricB:        92,
        LatestMetricC:        716,
        LatestMetricD:        78,
        LatestMetricE:        66,
        SiteName:             "Montreal",
        SiteID:               "2",
        PostCampWeek1:        "10/2/2018",
        PostCampWeek2:        "10/9/2018",
        PostCampWeek3:        "10/15/2018",
    }
    
  • 写回答

1条回答 默认 最新

  • douweiluo0600 2018-10-22 21:55
    关注

    Write the additional info, the agents and the closing brace to the response:

     io.WriteString(w, "{")
     io.WriteString(w, AdditionalInfo)
     json.NewEncoder(w).Encode(AllAgents)
     io.WriteString(w, "]}")  // closing ] for data and closing } for root
    

    Note this encodes two levels of [ ] around the agents. It's unclear if that's what you want.

    An alternate approach is to create a Go value matching the structure of the output and encode that:

    v := map[string]interface{}{
        "meta": map[string]interface{}{
            "page":    1,
            "pages":   1,
            "perpage": -1,
            "total":   350,
            "sort":    "asc",
            "field":   "RecordID",
        },
        "data": AllAgents,
     }
     if err := json.NewEncoder(w).Encode(v); err != nil {
        // handle error
     }
    

    Again, it's unclear how much nesting you want around agents. You may need this variation:

    v := map[string]interface{}{
        "meta": map[string]interface{}{
            "page":    1,
            "pages":   1,
            "perpage": -1,
            "total":   350,
            "sort":    "asc",
            "field":   "RecordID",
        },
        "data": []interface{}{AllAgents},
     }
     if err := json.NewEncoder(w).Encode(v); err != nil {
        // handle error
     }
    

    re: Efficiency Creating a slice of the final size is generally more efficient then incrementally growing the slice with append. There may be some efficenty gained by using a slice of pointers []*SingleAgent instead of the slice of struct values []SingleAgent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件