深入了解Golang包的獲取方法
1.獲取遠(yuǎn)程包
go 語(yǔ)言有一個(gè)獲取遠(yuǎn)程包的工具就是 go get,目前 go get 支持多數(shù)開(kāi)源社區(qū) (例如:github、googlecode、bitbucket、Launchpad)
例如:
go get github.com/astaxie/beedb
在pkg目錄下tree /f可查看到安裝的包如下所示:

2.應(yīng)用
如下代碼所示,可以應(yīng)用遠(yuǎn)程下載的go第三方庫(kù)文件,連接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)
}到此這篇關(guān)于深入了解Golang包的獲取方法的文章就介紹到這了,更多相關(guān)Golang包獲取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
windows下使用vscode搭建golang環(huán)境并調(diào)試的過(guò)程
這篇文章主要介紹了在windows下使用vscode搭建golang環(huán)境并進(jìn)行調(diào)試,主要包括安裝方法及環(huán)境變量配置技巧,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
Go語(yǔ)言如何實(shí)現(xiàn)限制用戶(hù)請(qǐng)求
這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言如何實(shí)現(xiàn)限制用戶(hù) 1 分鐘內(nèi)最多請(qǐng)求 1000 次,文中為大家整理了三個(gè)常用的方法,希望對(duì)大家有所幫助2025-01-01
Go語(yǔ)言題解LeetCode724尋找數(shù)組的中心下標(biāo)
這篇文章主要為大家介紹了Go語(yǔ)言題解LeetCode724尋找數(shù)組的中心下標(biāo),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
GoFrame基于性能測(cè)試得知grpool使用場(chǎng)景
這篇文章主要為大家介紹了GoFrame基于性能測(cè)試得知grpool使用場(chǎng)景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
在?Go?語(yǔ)言中使用?regexp?包處理正則表達(dá)式的操作
正則表達(dá)式是處理字符串時(shí)一個(gè)非常強(qiáng)大的工具,而?Go?語(yǔ)言的?regexp?包提供了簡(jiǎn)單而強(qiáng)大的接口來(lái)使用正則表達(dá)式,本文將介紹如何在?Go?中使用?regexp?包來(lái)編譯和執(zhí)行正則表達(dá)式,以及如何從文本中匹配和提取信息,感興趣的朋友一起看看吧2023-12-12

