从Go中的url资源读取

chi*_*may 2 url go

我们如何从url资源中读取。我在以下示例中使用了https://github.com/Kissaki/rest.go api。以下是我用于向URL http:// localhost:8080 / cool中写入字符串的示例, 但是现在我需要从URL中检索数据,如何将其读回?

package main

import (
    "fmt"
    "http"
    "github.com/nathankerr/rest.go"
)

type FileString struct {
    value string
}

func (t *FileString) Index(w http.ResponseWriter) {
    fmt.Fprintf(w, "%v", t.value)
}

func main(){
    rest.Resource("cool", &FileString{s:"this is file"})    
    http.ListenAndServe(":3000", nil)   
}
Run Code Online (Sandbox Code Playgroud)

小智 5

如果您只想通过http提取文件,则可以这样做

resp, err := http.Get("http://your.url.com/whatever.html")
check(err) // does some error handling

// read from resp.Body which is a ReadCloser
Run Code Online (Sandbox Code Playgroud)