duancan9815 2018-06-05 09:16
浏览 450
已采纳

如何在GoLang中创建多维键值数组/切片

I have a PHP script in which i'hv created some array formats (data structures) which i want to convert the same array structure using Golang.

Below is the array structure of my PHP script

   $response['spf']['current_value']  = $spfValue; // this will  be the array of strings
   $response['spf']['required_value'] = "v=spf1 a include:32782.pppp.com ~all";            
   $response['spf']['is_verified']    = $isValidSpf; //this will be int
   $response['spf']['spf_matched']    = $isMatched;  //this will be int
   print_r($response);

The outut of above script will be Array of key named SPF


[spf] => Array
    (
        [current_value] => Array
            (
                [0] => v=spf1 a -all,
            )
        [required_value] => v=spf1 a  include:32782.pppp.com ~all
        [is_verified] => 0
        [spf_matched] => 0
    )

As i am a new in golang , Need some golang code which will return same output as per PHP script

  • 写回答

1条回答 默认 最新

  • dsl2014 2018-06-05 13:16
    关注

    Hope below code will help you out. Make struct according to your JSON.

    package main

    import (
        "encoding/json"
        "fmt"
    )   
    
    type Resp struct {
        Spf Param `json:"spf"`
    }   
    
    type Param struct {
        Is_verified    int            `json:"is_verified"`
        Spf_matched    int            `json:"spf_matched"`
        Required_value string         `json:"required_value"`
        Current_value  map[int]string `json:"current_value"`
    }   
    
    func main() {
    
        str := make(map[int]string)
    
        str[0] = "v=spf1 a -all,"
        resp := Resp{Spf: Param{Is_verified: 0, Spf_matched: 0, Required_value: "v=spf1 a  include:32782.pppp.com ~all", Current_value: str}}
        js, _ := json.Marshal(resp)
        fmt.Printf("%s", js) 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?