我是非常新的去/编程在一般情况下-刚刚捡起来,而混乱的创建我自己的密码货币投资组合网站。
我正在努力打印到web服务器输出。如果我使用Printf -它打印到控制台,但一旦我使用Fprintf打印到web应用程序,我得到一些错误,我似乎无法解决。
有人能陪我走过吗?
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Obsidian []struct {
PriceUsd string `json:"price_usd"`
PriceBtc string `json:"price_btc"`
}
func webserver(w http.ResponseWriter, r *http.Request) {
url := "https://api.coinmarketcap.com/v1/ticker/obsidian/"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Do: ", err)
return
}
defer resp.Body.Close()
var record Obsidian
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
log.Println(err)
}
fmt.Printf("%+v", record)
}
func main() {
http.HandleFunc("/test", webserver)
http.ListenAndServe(":8001", nil)
}我试图取代:
fmt.Printf("%+v", record)通过以下方式:
fmt.Fprintf("%+v", record)并收到以下错误:
./test.go:54:21: cannot use "%+v" (type string) as type io.Writer in argument to fmt.Fprintf:
string does not implement io.Writer (missing Write method)
./test.go:54:21: cannot use record (type Obsidian) as type string in argument to fmt.Fprintf发布于 2017-10-15 23:05:23
感谢@MiloChrisstiansen
fmt.Fprintf(w, "%+v", record)发布于 2017-10-16 18:55:33
你也可以用
w.Write([]byte(record))https://stackoverflow.com/questions/46760795
复制相似问题