```go
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
type Users struct {
Username string
Password string
}
func main() {
user := Users{
"pengjin",
"123456",
}
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
encoder.Encode(user)
userBytes := result.Bytes()
//fmt.Println(userBytes)
var account Users
decoder := gob.NewDecoder(bytes.NewReader(userBytes))
decoder.Decode(&account)
fmt.Println(account.Username, account.Password)
}
```
有疑问加站长微信联系(非本文作者))