兄弟连Go语言教程之包net/http
所有的新技术都经历了一些成长的痛苦,区块链技术也一样。但是,尽管它还处于发展的早期阶段,但它具有改进许多行业、许多应用和系统的巨大潜力。
Go语言具有其关键特性。Go语言之所以如此强大,是因为它在服务端的开发中总能抓住程序员的痛痒需求,用最直接、简单、高效、稳定的方式来解决并实现问题。
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type UserInfo struct {
ApiKey string `json:"apiKey"`
UserId string `json:"userId"`
}
type Perception struct {
InputText map[string]string `json:"inputText,omitempty"`
InputImage map[string]string `json:"inputImage,omitempty"`
SelfInfo map[string]*RequestLocation `json:"selfInfo,omitempty"`
}
type RequestLocation struct {
City string `json:"city,omitempty"`
Province string `json:"province,omitempty"`
Street string `json:"street,omitempty"`
}
type RequestStruct struct {
ReqType int `json:"reqType"`
UserInfo *UserInfo `json:"userInfo"`
Perception *Perception `json:"perception"`
}
type Test1 struct {
Id string
Age string
Class string
}
type Test struct {
Name string
Test1 Test1
}
func main() {
apiAddress := "
requeStruct := &RequestStruct{0,
&UserInfo{ApiKey: "231ae8807c384f41805802bdd4973638", UserId: "123456111"},
&Perception{InputText: map[string]string{"text": "今天的天气如何"}, SelfInfo: map[string]*RequestLocation{"location": &RequestLocation{City: "北京"}}}}
jsonByte, err := json.Marshal(requeStruct)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(string(jsonByte))
}
request, err := http.NewRequest("POST", apiAddress, bytes.NewBuffer(jsonByte))
if err != nil {
fmt.Println(err.Error())
}
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
client := http.Client{}
resp, err := client.Do(request)
if err != nil {
fmt.Println(err.Error())
return
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(respBytes))
}
高能预警,兄弟连教育区块链直播课程8月持续火爆来袭!
原价1188元的12节区块链进阶课程,现仅需1元!
还可免费领取《Go语言基础实战项目开发》与《Go语言高级实战项目开发》教材两本!!
限时限量!!先到先得!!
http://www.ydma.cn/open/course/24
!!!
网上报名
新闻资讯
更多>>-
兄弟连“因材施教搭建桥梁”计划正式启动
2018-06-14
-
兄弟连PHP培训:高薪就业才是硬道理
2018-06-14
-
兄弟连北京安卓培训:万元高薪班级爆满
2018-06-14
-
25W高薪挑战安卓 零基础培训首选兄弟连
2018-06-14
-
兄弟连PHP教程发布 海量资源应有尽有
2018-06-14