培训首页  >  web前端新闻  >  兄弟连Go语言教程之包net/http

兄弟连Go语言教程之包net/http

[2018-08-24 11:36:13] 浏览量:63 来源:

北京兄弟连IT

  所有的新技术都经历了一些成长的痛苦,区块链技术也一样。但是,尽管它还处于发展的早期阶段,但它具有改进许多行业、许多应用和系统的巨大潜力。

 

  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

!!!



文中图片素材来源网络,如有侵权请联系删除
  • 软件开发
  • 软件测试
  • 数据库
  • Web前端
  • 大数据
  • 人工智能
  • 零基础
  • 有HTML基础
  • 有PHP基础
  • 有C语言基础
  • 有JAVA基础
  • 其他计算机语言基础
  • 周末班
  • 全日制白班
  • 随到随学

网上报名

热门信息