如何在 Go 语言中创建多维键值 array/slice

How to create Multidimensional Key Value array/slice in GoLang

我有一个 PHP 脚本,我在其中创建了一些数组格式(数据结构),我想使用 Golang.

转换相同的数组结构

下面是我的PHP脚本的数组结构

   $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);

以上脚本的输出将是名为 SPF 的密钥的 Array

[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 )

因为我是 golang 的新人,需要一些 golang 代码,它将 return 与每个 PHP 脚本

相同的输出

希望下面的代码对您有所帮助。根据您的 JSON 制作结构。

主要包

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) 
}