Go interfacer转为map

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

func HttpGet(url string)(errCode int, result map[string]interface{}){
	resp, err := http.Get(url)
	if err != nil{

		return 500, map[string]interface{}{
			"state":err.Error(),
		};
	}

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)
	json.Unmarshal(body, &result)
	return 200, result
}

func main(){
	_ , body := HttpGet( "http://timor.tech/api/holiday/info/" + time.Now().Format("2006-01-02"))

	cc := body["holiday"]

	aa := cc.(map[string]interface{})
	fmt.Print(aa, aa["holiday"])
}

发布了254 篇原创文章 · 获赞 70 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/zhizhengguan/article/details/104036587