douqiao5543 2018-08-07 10:08
浏览 24

从字符串到结构Golang

I created a function to transform a particular string (derived from [] bytes) into a struct. In the program I am building an encryption and a decryption are carried out and between these two passages the data are transferred via JSON. The function that I propose works, but at the time when the string becomes very long, processing times are very long. What I would like to ask is if anyone has any idea how to speed up this function or how to manage the situation better.

this is the function

func Datadecrypt(input []byte) Data {

    s := string(input)

    vals := strings.Replace(s,"  "," ",-1)

    part := strings.Fields(vals)

    c := strings.Split(s, "int=")[1]
    co := strings.Split(c, ")")[0]

    count, err := strconv.Atoi(co)
    if err != nil {
        log.Fatal(err)
    }

    var stringa string

    for j := 0; j<len(part); j++ {
        if strings.ContainsAny(part[j], ":") {
            stringa = stringa+" "+part[j]
        } else {
            stringa = stringa+","+part[j]
        }
    }

    var interpart []map[string]string
    var result Data

    var i = 0

    for {

        split := strings.Split(stringa, "[")[2+i]

        splitend := strings.Split(split, "]")[0]

        control := strings.Replace(splitend,"  "," ",-1)

        mapst := mapstring.StrToMS(control)

        interpart = append(interpart, mapst)

        i++

        if i >= count {
            break
        }
    }

    result = Data{Count: count, Results: interpart}

    return result
}

this is the struct

type Data struct {
    Count int `json:"count"`
    Results []map[string]string `json:"result"`
}

this is the string that derives from s: = string (input)

"Count=\"%!s(int=107)\"
Results=\"[map[MATRICOLA:2 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:7 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:5 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:6 NOMEmy:value COGNOMEmy:value]]\"
"

The string I wrote above is printed using JSON response

This is the input string NOT JSON

Count="%!s(int=107)"
Results="[map[MATRICOLA:2 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:7 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:5 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:6 NOMEmy:value COGNOMEmy:value]]"

this is the result I get through the function

{107 map[MATRICOLA:2 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:7 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:5 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:6 NOMEmy:value COGNOMEmy:value]]}

Thanks, in advance

  • 写回答

1条回答 默认 最新

  • duanou9758 2018-08-07 13:01
    关注

    This is not a answer to your question, but maybe a solution to your problem. Just leave a comment if I am totaly wrong.

    The input string looks like it is generated via fmt.Printf()

    "Count=\"%!s(int=107)\"
    Results=\"[map[MATRICOLA:2 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:7 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:5 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:6 NOMEmy:value COGNOMEmy:value]]\"
    "
    

    https://play.golang.org/p/pRWUtRKikEh

        count := 107
        results := "[map[MATRICOLA:2 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:7 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:5 NOMEmy:value COGNOMEmy:value] map[MATRICOLA:6 NOMEmy:value COGNOMEmy:value]]"
    
        fmt.Printf("Count=%s
    Results=%s", count, results)
    

    So if you would be able to change the print statement to fmt.Printf("Count=%d Results=%s", count, results) the output would be correct. https://play.golang.org/p/g45_TiHMFsG

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?