【发布时间】:2018-02-05 14:23:43
【问题描述】:
我正在练习 golang,但我不知道如何捕捉错误。
我的期望:
-
FetchTickerData运行 - 它同时调用2个不同的函数:
fetchPriceTicket和fetchWhatToMine - 如果其中一个函数返回错误,则
FetchTickerData返回该错误 - 如果一切正常,它会处理来自两个来源的数据并返回
我不知道如何捕捉错误。我写了这段代码,但我认为它不是正确的解决方案并且它不起作用。有什么更好的方法来做到这一点?
package main
import "net/http"
import (
"github.com/tidwall/gjson"
"time"
"io/ioutil"
"fmt"
)
var client = &http.Client{Timeout: 10 * time.Second}
type Ticker struct {
}
func FetchTickerData() (error, *gjson.Result, *gjson.Result) {
whatToMine := make(chan *gjson.Result)
currency := make(chan *gjson.Result)
err := make(chan error)
counter := 0 // This variable indicates if both data was fetched
go func() {
innerError, json := fetchWhatToMine()
fmt.Print(innerError)
if innerError != nil {
err <- innerError
// Stop handler immediately
whatToMine <- nil
currency <- nil
return
}
whatToMine <- json
counter = counter + 1
if counter == 2 {
fmt.Print("err pushed")
err <- nil
}
}()
go func() {
innerError, json := fetchPriceTicket()
fmt.Print(innerError)
if innerError != nil {
err <- innerError
whatToMine <- nil
currency <- nil
return
}
currency <- json
counter = counter + 1
if counter == 2 {
fmt.Print("err pushed")
err <- nil
}
}()
return <-err, <-whatToMine, <-currency
}
func fetchPriceTicket() (error, *gjson.Result) {
resp, err := client.Get("https://api.coinmarketcap.com/v1/ticker/")
if err != nil {
return err, nil
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json := gjson.GetBytes(body, "");
return nil, &json;
}
func fetchWhatToMine() (error, *gjson.Result) {
resp, err := client.Get("https://whattomine.com/coins.json")
if err != nil {
return err, nil
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json := gjson.GetBytes(body, "");
return nil, &json;
}
UPD:如果我将 return <-err, <-whatToMine, <-currency 替换为 return nil, <-whatToMine, <-currency,它会返回我期望的数据,但如果有则不会返回错误。
UPD:还有第二个版本的代码:
package main
import "net/http"
import (
"github.com/tidwall/gjson"
"time"
"io/ioutil"
"context"
"fmt"
)
var client = &http.Client{Timeout: 10 * time.Second}
type Ticker struct {
}
func main() {
ticker, coins, err := FetchTickerData()
fmt.Print("Everything is null! ", ticker, coins, err)
if err != nil {
fmt.Print(err)
return
}
fmt.Print("Bitcoin price in usd: ", ticker.Array()[0].Get("price_usd"))
}
func FetchTickerData() (*gjson.Result, *gjson.Result, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var result1, result2 *gjson.Result
var err1, err2 error
go func() {
result1, err1 = fetchJson(ctx, "https://api.coinmarketcap.com/v1/ticker/")
if err1 != nil {
cancel() // Abort the context, so the other function can abort early
}
}()
go func() {
result2, err2 = fetchJson(ctx, "https://whattomine.com/coins.json")
if err2 != nil {
cancel() // Abort the context, so the other function can abort early
}
}()
if err1 == context.Canceled || err1 == nil {
return result1, result2, err2
}
return result1, result2, err1
}
func fetchJson(ctx context.Context, url string) (*gjson.Result, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
fmt.Print("I don't know why this body isn't printed ", string(body))
json := gjson.ParseBytes(body)
return &json, nil
}
由于某些原因,http 请求在此处无法正常工作,并且没有错误。想法?
Everything is null! <nil> <nil> <nil>panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x11f7843]
goroutine 1 [running]:
main.main()
/Users/andrey/go/src/tickerUpdater/fetchTicker.go:25 +0x183
【问题讨论】:
-
确保您始终将错误作为函数的 last 值返回。
-
@Flimzy 哪个参数(最后一个或第一个或第二个)是错误的重要吗?
-
是的。 Go 的约定是错误总是最后一个。否则标准 Go 工具会抱怨。
-
你也应该使用'go fmt'——它将提供更统一的编码约定,并删除不必要的分号等。
标签: go