This is a creation in Article, where the information may have evolved or changed.
26. Notes Go language--beedb library use
Beedb is a go-to-ORM library that uses go style to manipulate the database, enabling the mapping of a struct to a data table record. is a very lightweight go ORM framework that reduces the complexity of the ORM learning curve and seeks to balance the efficiency and functionality of the ORM as much as possible.
Installation
Support for Go get mode installation, is completely in accordance with the way the go style.
Command line execution: Go get github.com/astaxie/beedb
Requires import of the appropriate database driver package, Database/sql standard interface package, and Beedb package
Import (
"Database/sql"
"Github.com/astaxie/beedb"
_ "Github.com/ziutek/mymysql/godrv"
)
Initialization
DB, err: = SQL. Open ("Mymysql", "test/xiemengjun/123456")
If err! = Nil {
Panic (ERR)
}
ORM: = Beedb. NEW (DB)
Beedb's new function should actually have two parameters, the first parameter is the standard interface of the DB, the second parameter is the database engine used, if you use the database engine is Mysql/sqlite, then the second parameter can be omitted.
If the database you are using is SQL Server, then the initialization requires:
ORM = Beedb. New (DB, "MSSQL")
If you are using PostgreSQL, then the initialization requires:
ORM = Beedb. New (DB, "PG")
Using the previous database table UserInfo, we now establish the corresponding struct
Type Userinfo struct {
Uid int ' PK '//If the table's primary key is not an ID, then you need to add the PK comment, which explicitly says this field is the primary key
Username string
Departname string
Created time. Time
}
Beedb for the hump name will automatically help you to translate into an underscore field, for example, you define the struct name is UserInfo, then to the bottom of the implementation is User_info, field naming also follows the rule.
Inserting data
How to insert a record, we can see that we are manipulating the Strcut object, not the native SQL statement, and finally save the data to the database by calling the Save interface.
var saveone Userinfo
Saveone. Username = "Test AddUser"
Saveone. Departname = "Test adddepartname" Saveone. Created = time. Now ()
Orm. Save (&saveone)