forked from zxysilent/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package fetch | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// http 请求用 | ||
// 总计超时时间限制5s | ||
var client = http.Client{Timeout: 5 * time.Second} | ||
|
||
// Get HTTP 工具类, GET 并解析返回的报文,如果有错误,返回 error | ||
// url 统一资源定位符(uniform resource locator;URL) | ||
// reply 返回值 | ||
func Get(url string, reply interface{}) error { | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
return json.NewDecoder(resp.Body).Decode(reply) | ||
} |