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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面