-
StackOverflow 文件
-
Go 教程
-
凝塊
-
如何使用 gob 對介面進行編碼
package main
import (
"encoding/gob"
"fmt"
"os"
)
type User struct {
Username string
Password string
}
type Admin struct {
Username string
Password string
IsAdmin bool
}
type Deleter interface {
Delete()
}
func (u User) Delete() {
fmt.Println("User ==> Delete()")
}
func (a Admin) Delete() {
fmt.Println("Admin ==> Delete()")
}
func main() {
user := User{
"zola",
"supersecretpassword",
}
admin := Admin{
"john",
"supersecretpassword",
true,
}
file, _ := os.Create("user.gob")
adminFile, _ := os.Create("admin.gob")
defer file.Close()
defer adminFile.Close()
gob.Register(User{}) // registering the type allows us to encode it
gob.Register(Admin{}) // registering the type allows us to encode it
encoder := gob.NewEncoder(file)
adminEncoder := gob.NewEncoder(adminFile)
InterfaceEncode(encoder, user)
InterfaceEncode(adminEncoder, admin)
}
func InterfaceEncode(encoder *gob.Encoder, d Deleter) {
if err := encoder.Encode(&d); err != nil {
fmt.Println(err)
}
}