Created
April 5, 2020 01:38
-
-
Save foolishway/d37811834b872123d0b89245f02426cb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"time" | |
) | |
type MyUser struct { | |
ID int64 `json:"id"` | |
Name string `json:"name"` | |
LastSeen time.Time `json:"lastSeen"` | |
} | |
func (u *MyUser) MarshalJSON() ([]byte, error) { | |
//return json.Marshal( | |
// &struct { | |
// ID int64 `json:"id"` | |
// Name string `json:"name"` | |
// LastSeen string `json:"lastSeen"` | |
// }{ | |
// ID: u.ID, | |
// Name: u.Name, | |
// LastSeen: u.LastSeen.Format("2006-01-02 15:04:05"), | |
// }) | |
// return json.Marshal( | |
// &struct { | |
// *MyUser | |
// LastSeen string `json:"lastSeen"` | |
// }{ | |
// MyUser: u, | |
// LastSeen: u.LastSeen.Format("2006-01-02 15:04:05"), | |
// }, | |
// ) | |
type Alias MyUser | |
return json.Marshal( | |
&struct { | |
*Alias | |
LastSeen string `json:"lastSeen"` | |
}{ | |
Alias: (*Alias)(u), | |
LastSeen: u.LastSeen.Format("2006-01-02 15:04:05"), | |
}, | |
) | |
} | |
type cUser MyUser | |
func main() { | |
cUser := &cUser{123, "zhangwei", time.Now()} | |
fmt.Println(cUser.ID) | |
// u := &MyUser{1, "Ken", time.Now()} | |
// bs, _ := json.Marshal(u) | |
// os.Stdout.Write(bs) | |
f := func(res string) { | |
fmt.Println(res) | |
} | |
doAsync(f) | |
} | |
func doAsync(callback func(string)) { | |
t := time.NewTimer(2 * time.Second) | |
<-t.C | |
callback("response") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment