bson的介绍不说了
golang下的解析包找到2个 一个是mongo的http://labix.org/gobson
,另外一个比较小众https://github.com/sbunce/bson

这里用的是mongo的作为例子。
对象加上不同的注解,
可以轻松转成xml json bson 想想都兴奋 

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "labix.org/v2/mgo/bson"
 6 )
 7 
 8 type TestStruct struct {
 9     Name string
10     ID   int32
11 }
12 
13 func main() {
14     fmt.Println("start")
15     data, err := bson.Marshal(&TestStruct{Name: "Bob"})
16     if err != nil {
17         panic(err)
18     }
19     fmt.Println("%q", data)
20 
21     value := TestStruct{}
22     err2 := bson.Unmarshal(data, &value)
23     if err2 != nil {
24         panic(err)
25     }
26     fmt.Println("value:", value)
27 
28     mmap := bson.M{}
29     err3 := bson.Unmarshal(data, mmap)
30     if err3 != nil {
31         panic(err)
32     }
33     fmt.Println("mmap:", mmap)
34 }

 

转载自 http://www.blogjava.net/oathleo/archive/2013/09/22/golang_bson_struct_mongo.html

相关文章:

  • 2021-12-29
  • 2021-05-27
  • 2021-07-20
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-27
  • 2021-11-18
  • 2022-12-23
  • 2022-01-02
  • 2021-11-06
  • 2022-12-23
  • 2021-09-02
相关资源
相似解决方案