douyinghuo8874 2019-05-08 01:51
浏览 22
已采纳

访问链接后获取输出字符串

I am writing a program by Go. In this program, I access to a website and in this website, it will print a string. I want to get this string for next process. For example:

I access by curl and the returned string will like that:

curl localhost:4000
abc_example

I need to get "abc_example" for next process in my program. Now, this problem was solved.

Actually, my result will be a JSON like that:

{"name":"xyz_example"}

How can I parse this string and just get "xyz_example"

I am a newbie in Go. May you help me. Thank you!

  • 写回答

1条回答 默认 最新

  • doubing3662 2019-05-08 02:08
    关注

    Here's an example of reading the response from an HTTP request. I would recommend reading up on the documentation for the http package, and maybe a simple tutorial like this one.

    package main
    
    import (
        "fmt"
        "net/http"
        "io/ioutil"
    )
    
    func main() {
        //make a request
        response, err := http.Get("https://mdtf.org")
        if err != nil {
          fmt.Println("error making request: ", err)
          return
        }
    
        //make sure the response body gets closed
        defer response.Body.Close()
    
        //read the bytes
        responseBytes, err := ioutil.ReadAll(response.Body)
        if err != nil {
          fmt.Println("error reading response bytes: ", err)
          return
        }
    
        //turn the response bytes into a string
        responseString := string(responseBytes)
    
        //print it or something
        fmt.Println(responseString)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?