這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package dalimport ( "encoding/json" "strconv" "strings" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql")type automethod struct { ActionId int `orm:"pk;auto;column(ActionId)"` ActionName string `orm:"size(150);column(ActionName)"` ActionArg string `orm:"size(2000);column(ActionArg)"` ActionType int `orm:"size(11);column(ActionType)"` MethodRunName string `orm:"size(150);column(MethodRunName)"` AssemblyId int `orm:"size(11);column(AssemblyId)"` ClassName string `orm:"size(150);column(ClassName)"` SubActionId int `orm:"size(11);column(SubActionId)"` ActionRuntime string `orm:"size(150);column(ActionRuntime)"`}func init() { orm.RegisterModelWithPrefix("", new(automethod)) orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDataBase("default", "mysql", "root:123456@tcp(10.101.42.64:3306)/mytest?charset=utf8")}func PrintActionJson() interface{} { o := orm.NewOrm() o.Using("mytest") var actionInfos []*automethod sel := o.QueryTable("automethod") count, err := sel.All(&actionInfos) if err != nil { return "條數" + string(count) + err.Error() } //json轉換 jsonData, err := json.Marshal(actionInfos) if err != nil { return err.Error() } newdata := make([]automethod, len(actionInfos)) json.Unmarshal(jsonData, &newdata) return string(jsonData)}func PrintActionModel() interface{} { o := orm.NewOrm() o.Using("mytest") var actionInfos []*automethod sel := o.QueryTable("automethod") count, err := sel.All(&actionInfos) if err != nil { return "條數" + string(count) + err.Error() } //json轉換 jsonData, err := json.Marshal(actionInfos) if err != nil { return err.Error() } newdata := make([]automethod, len(actionInfos)) json.Unmarshal(jsonData, &newdata) return actionInfos}func PrintActionIdAndName() interface{} { o := orm.NewOrm() o.Using("mytest") var actionIds []int var actionNames []string raw := o.Raw("select ActionId,ActionName from automethod") num, err := raw.QueryRows(&actionIds, &actionNames) var rowsData = make([]string, num, 100) if err == nil { for i := int64(0); i < num; i++ { rowsData[i] = strconv.Itoa(actionIds[i]) + actionNames[i] } return strings.Join(rowsData, ",") } return err.Error()}