package wechatx import ( "encoding/json" "fmt" "io/ioutil" "net/http" ) func (w *wechat) GetAccessToken() { appid := "" secret := "" url := fmt.Sprintf("%s/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", WechatHost, appid, secret) resp, err := http.Get(url) if err != nil { // } defer resp.Body.Close() by, err := ioutil.ReadAll(resp.Body) if err != nil { // } var response struct { ErrCode int64 `json:"errcode"` ErrMsg string `json:"errmsg"` AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` } json.Unmarshal(by, &response) if response.ErrCode != 0 { // } }