-3

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

oguz ismail
  • 1
  • 16
  • 47
  • 69
sabby
  • 41
  • 1
  • 5
  • Hi sid, what is exact requirement ? can you explain bit more ? – Ashish Tiwari Jun 05 '18 at 11:14
  • Hi Ashish, i am using Golang's GIN framework for development of REST API's. In which i am facing the problem in making the associative array. For a reference i have mentioned my PHP code which i want to convert in go format. – sabby Jun 05 '18 at 11:20

1 Answers1

3

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) 
}
Ashish Tiwari
  • 1,919
  • 1
  • 14
  • 26