bson的介绍不说了golang下的解析包找到2个 一个是mongo的http://labix.org/gobson,另外一个比较小众https://github.com/sbunce/bson这里用的是mongo的作为例子.对象加上不同的注解,可以轻松转成xml json bson 想想都兴奋 package main import ( "fmt" "labix.org/v2/mgo/bson" ) type TestStruct struct { Name st
6.2 Go 匿名字段 Golang匿名字段:可以像访问字段成员那样,访问匿名字段方法,go编译器自动查找. package main import "fmt" type Student struct { id int name string } type Teacher struct { //匿名字段 Student } func (s1 *Student) Play() { fmt.Printf("Student地址:%p,值是%v\n", s1, s1) fm
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/125 假设我们有如下结构体: type User struct { Id int Name string Bio string Email string } 我们需要对结构体内的字段进行验证合法性: Id的值在某一个范围内. Name的长度在某一个范围内. Email格式正确. 我们可能会这么写: user := User{ Id: 0, Name: "supe
type TA struct { Id int64 `db:"id"` } type TB struct { Id int64 `db:"id"` } type TC struct { TA TB } 如上的sql和结构体, 当查询sql,用TC结构体封装时, 有两个id字段, 那么数据库中id的值是在TB的id中 如果TC的结构体是 type TC struct { TB TA } 这样, 那么id的值就是在TA中