亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

golang:如何在循環中填充多結構映射?

golang:如何在循環中填充多結構映射?

Go
墨色風雨 2021-09-09 15:18:00
我有客戶與 API 交互的日志文件。我想解析這些日志并將結果提供給結構映射,以便我可以將數據組織成有用的信息。例如,我想響應以下查詢:“顯示每個用戶每天的請求總數”。我已經創建了一個看起來足夠的結構來保存數據。但是,當我嘗試運行該程序時,出現錯誤:invalid operation: dates[fields[1]] (type *Dates does not support indexing) [process exited with non-zero status].http://play.golang.org/p/8u3jX26kttpackage mainimport (    "fmt"    "strings")type Stats struct {    totalNumberOfRequests int}type Customer struct {    listOfCustomers map[string]Stats // map[customerid]Stats}type Dates struct {    listOfDates map[string]Customer // map[date]Customer}var requestLog = []string{    "2011-10-05, 1234, apiquery",    "2011-10-06, 1234, apiquery",    "2011-10-06, 5678, apiquery",    "2011-10-09, 1234, apiquery",    "2011-10-12, 1234, apiquery",    "2011-10-13, 1234, apiquery",}func main() {    dates := new(Dates)    for _, entry := range requestLog {        fmt.Println("entry:", entry)        fields := strings.Split(entry, "'")        dates.listOfDates[fields[0]].listOfCustomers[fields[1]].totalNumberOfRequests++    }}有沒有更好的結構可以使用?或者有沒有辦法讓這個結構適合這個特定目的?
查看完整描述

1 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

如果我理解您對輸出的期望,這里有一個解決方案。但是我不喜歡“客戶是一個帶有 id 和 Stat 的地圖......我認為它應該是一個更簡單的結構,有兩個字段(cid string和stat Stats)。而且日期結構不允許在同一日期有多個客戶,所以我已更改為將單個日期映射到用戶列表。


我還添加了更多“測試場景”以涵蓋客戶在同一日期多次訪問資源的情況。


您似乎沒有使用您的示例中的“apiquery”,因此下面的代碼與它不匹配。


關于結構中指針的更改 - 請參閱此問題(如對您的問題的評論所述)


package main


import (

    "fmt"

    "strings"

)


type Stats struct {

    totalNumberOfRequests int

}

type Customer struct {

    customerWithStat map[string]*Stats // a customer with it's corresponding stats

}


type Dates struct {

    listOfDates map[string][]*Customer // map[date]list of customers (for each date)

}


var requestLog = []string{

    "2011-10-05, 1234, apiquery",

    "2011-10-06, 5678, apiquery",

    "2011-10-06, 1234, apiquery",

    "2011-10-06, 1234, apiquery",

    "2011-10-06, 5678, apiquery",

    "2011-10-06, 1234, apiquery",

    "2011-10-09, 1234, apiquery",

    "2011-10-12, 1234, apiquery",

    "2011-10-13, 1234, apiquery",

    "2011-10-13, 1234, apiquery",

    "2011-10-06, 1234, apiquery",

}


func main() {

    listOfDates := make(map[string][]*Customer)

    dates := Dates{listOfDates}

    for _, entry := range requestLog {

        fields := strings.Split(entry, ",")

        curDateStr := strings.TrimSpace(fields[0])

        curCustIdStr := strings.TrimSpace(fields[1])


        if customersAtDate, dateExists := dates.listOfDates[curDateStr]; dateExists {

            // Date already exist

            for _, curCustomer := range customersAtDate {

                if curStat, customerExists := curCustomer.customerWithStat[curCustIdStr]; customerExists {

                    // The user has already accessed this resource - just increment

                    curStat.totalNumberOfRequests++

                } else {

                    // New user - set access to 1

                    curCustomer.customerWithStat[curCustIdStr] = &Stats{1}

                }

            }

        } else {

            // New Date


            // Init the Statistic for the new customer

            newCustomerData := make(map[string]*Stats)

            newCustomerData[curCustIdStr] = &Stats{1}


            // Create the customer itself

            newCustomer := &Customer{newCustomerData}


            // add to the current day list

            dates.listOfDates[curDateStr] = append(dates.listOfDates[curDateStr], newCustomer)


        }

    }


    // Print result

    for date, customers := range dates.listOfDates {

        fmt.Println("Date: ", date)

        for _, customer := range customers {

            for cid, stat := range customer.customerWithStat {

                fmt.Println("  Customer: ", cid)

                fmt.Println("  # Requests: ", *stat)

            }

        }

    }

}

這將輸出:


Date:  2011-10-05

  Customer:  1234

  # Requests:  {1}

Date:  2011-10-06

  Customer:  5678

  # Requests:  {2}

  Customer:  1234

  # Requests:  {4}

Date:  2011-10-09

  Customer:  1234

  # Requests:  {1}

Date:  2011-10-12

  Customer:  1234

  # Requests:  {1}

Date:  2011-10-13

  Customer:  1234

  # Requests:  {2}


查看完整回答
反對 回復 2021-09-09
  • 1 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號