type MyStruct struct {
Val int
}
func myfunc() MyStruct {
return MyStruct{Val: 1}
}
func myfunc() *MyStruct {
return &MyStruct{}
}
func myfunc(s *MyStruct) {
s.Val = 1
}
The first returns a copy of the struct, the second a pointer to the struct value created within the function, the third expects an existing struct to be passed in and overrides the value.
package main
import (
"fmt"
"reflect"
)
type test struct {
a int
b int
}
func main() {
vb := test{a: 12, b: 13}
v := reflect.ValueOf(vb)
for idx := 0; idx < v.NumField(); idx++ {
keyNum := v.Field(idx)
fmt.Println(keyNum)
}
}
// 12
// 13
引入其他 package 的 struct
./user/schema.go
package user
type Schema struct {
ID int `json:"id"`
Name string `json:"name"`
Age string `json:"age"`
Account string `json:"account"`
Password string `json:"password"`
RegisterTime int64 `json:"registerTime"`
Gender string `json:"gender"`
City string `json:"city"`
}
main.go
import (
USER "./user"
)
func main() {
type User USER.Schema
....
}