網站首頁 編程語言 正文
1.獲取遠程包
go 語言有一個獲取遠程包的工具就是 go get
,目前 go get 支持多數開源社區 (例如:github、googlecode、bitbucket、Launchpad)
例如:
go get github.com/astaxie/beedb
在pkg目錄下tree /f可查看到安裝的包如下所示:
2.應用
如下代碼所示,可以應用遠程下載的go第三方庫文件,連接sqllite。
鏈接地址
package main import ( "fmt" "github.com/astaxie/beedb" _ "github.com/mattn/go-sqlite3" "time" "database/sql" ) /* CREATE TABLE `userinfo` ( `uid` INTEGER PRIMARY KEY AUTOINCREMENT, `username` VARCHAR(64) NULL, `departname` VARCHAR(64) NULL, `created` DATE NULL ); CREATE TABLE `userdeatail` ( `uid` INT(10) NULL, `intro` TEXT NULL, `profile` TEXT NULL, PRIMARY KEY (`uid`) ); */ var orm beedb.Model type Userinfo struct { Uid int `beedb:"PK"` Username string Departname string Created string } func main() { db, err := sql.Open("sqlite3", "./asta.db") if err != nil { panic(err) } orm = beedb.New(db) //insert() //insertsql() // a := selectone() // fmt.Println(a) // b := selectall() // fmt.Println(b) // update() // updatesql() // findmap() // groupby() // jointable() // delete() //deletesql() //deleteall() } func insert() { //save data var saveone Userinfo saveone.Username = "Test Add User" saveone.Departname = "Test Add Departname" saveone.Created = time.Now().Format("2006-01-02 15:04:05") orm.Save(&saveone) fmt.Println(saveone) } func insertsql() { // add one add := make(map[string]interface{}) add["username"] = "astaxie" add["departname"] = "cloud develop" add["created"] = "2012-12-02" orm.SetTable("userinfo").Insert(add) } func selectone() Userinfo { //get one info var one Userinfo orm.Where("uid=?", 1).Find(&one) return one } func selectall() []Userinfo { //get all data var alluser []Userinfo orm.Limit(10).Where("uid>?", 1).FindAll(&alluser) return alluser } func update() { // //update data var saveone Userinfo saveone.Uid = 1 saveone.Username = "Update Username" saveone.Departname = "Update Departname" saveone.Created = time.Now().Format("2006-01-02 15:04:05") orm.Save(&saveone) fmt.Println(saveone) } func updatesql() { //original SQL update t := make(map[string]interface{}) t["username"] = "updateastaxie" //update one orm.SetTable("userinfo").SetPK("uid").Where(2).Update(t) //update batch orm.SetTable("userinfo").Where("uid>?", 3).Update(t) } func findmap() { //Original SQL Backinfo resultsSlice []map[string][]byte //default PrimaryKey id c, _ := orm.SetTable("userinfo").SetPK("uid").Where(2).Select("uid,username").FindMap() fmt.Println(c) } func groupby() { //Original SQL Group By b, _ := orm.SetTable("userinfo").GroupBy("username").Having("username='updateastaxie'").FindMap() fmt.Println(b) } func jointable() { //Original SQL Join Table a, _ := orm.SetTable("userinfo").Join("LEFT", "userdeatail", "userinfo.uid=userdeatail.uid").Where("userinfo.uid=?", 1).Select("userinfo.uid,userinfo.username,userdeatail.profile").FindMap() fmt.Println(a) } func delete() { // // //delete one data saveone := selectone() orm.Delete(&saveone) } func deletesql() { //original SQL delete orm.SetTable("userinfo").Where("uid>?", 2).DeleteRow() } func deleteall() { // //delete all data alluser := selectall() orm.DeleteAll(&alluser) }
原文鏈接:https://juejin.cn/post/7119523086352777247
相關推薦
- 2022-04-18 python?dataframe實現統計行列中零值的個數_python
- 2022-11-13 使用git?checkout到歷史某個版本_相關技巧
- 2022-03-12 linux下scp和sftp命令使用詳解_Linux
- 2022-05-20 Docker容器鏡像加載及底層基本原理深入解析_docker
- 2022-05-22 xampp安裝后Apache無法啟動解決辦法_Linux
- 2022-08-21 golang中defer的基本使用教程_Golang
- 2024-03-07 Mapper批量映射優化
- 2023-06-20 Python?Unittest?ddt數據驅動的實現_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支