国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Go學(xué)習(xí)第九天

這篇具有很好參考價值的文章主要介紹了Go學(xué)習(xí)第九天。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

使用sqlite3

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
	_ "github.com/mattn/go-sqlite3"
	"log"
	"time"
)

var schema = `
CREATE TABLE person (
    first_name text,
    last_name text,
    email text
);

CREATE TABLE place (
    country text,
    city text NULL,
    telcode integer
)`

type Person struct {
	FirstName string `db:"first_name"`
	LastName  string `db:"last_name"`
	Email     string
}

type Place struct {
	Country string
	City    sql.NullString
	TelCode int
}

func main() {
	// this Pings the database trying to connect
	// use sqlx.Open() for sql.Open() semantics
	//dsn := "user:password@tcp(127.0.0.1:3306)/go-study?charset=utf8&parseTime=True"
	//db, err := sqlx.Connect("mysql", dsn)
	db, err := sqlx.Connect("sqlite3", "__deleteme.db")
	if err != nil {
		log.Fatalln(err)
	}
	db.SetConnMaxLifetime(time.Second * 10)
	db.SetMaxOpenConns(20) // 設(shè)置與數(shù)據(jù)庫建立連接的最大數(shù)目
	db.SetMaxIdleConns(10) // 設(shè)置連接池中的最大閑置連接數(shù)

	// exec the schema or fail; multi-statement Exec behavior varies between
	// database drivers;  pq will exec them all, sqlite3 won't, ymmv
	db.MustExec(schema)

	tx := db.MustBegin()
	tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "jmoiron@jmoiron.net")
	tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "johndoeDNE@gmail.net")
	tx.MustExec("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1")
	tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852")
	tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Singapore", "65")
	// Named queries can use structs, so if you have an existing struct (i.e. person := &Person{}) that you have populated, you can pass it in as &person
	tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "jane.citzen@example.com"})
	tx.Commit()

	// Query the database, storing results in a []Person (wrapped in []interface{})
	people := []Person{}
	db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
	jason, john := people[0], people[1]

	fmt.Printf("%#v\n%#v", jason, john)
	// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
	// Person{FirstName:"John", LastName:"Doe", Email:"johndoeDNE@gmail.net"}

	// You can also get a single result, a la QueryRow
	jason = Person{}
	err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
	fmt.Printf("%#v\n", jason)
	// Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}

	// if you have null fields and use SELECT *, you must use sql.Null* in your struct
	places := []Place{}
	err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
	if err != nil {
		fmt.Println(err)
		return
	}
	usa, singsing, honkers := places[0], places[1], places[2]

	fmt.Printf("%#v\n%#v\n%#v\n", usa, singsing, honkers)
	// Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
	// Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
	// Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}

	// Loop through rows using only one struct
	place := Place{}
	rows, err := db.Queryx("SELECT * FROM place")
	for rows.Next() {
		err := rows.StructScan(&place)
		if err != nil {
			log.Fatalln(err)
		}
		fmt.Printf("%#v\n", place)
	}
	// Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
	// Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
	// Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}

	// Named queries, using `:name` as the bindvar.  Automatic bindvar support
	// which takes into account the dbtype based on the driverName on sqlx.Open/Connect
	_, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`,
		map[string]interface{}{
			"first": "Bin",
			"last":  "Smuth",
			"email": "bensmith@allblacks.nz",
		})

	// Selects Mr. Smith from the database
	rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{"fn": "Bin"})

	// Named queries can also use structs.  Their bind names follow the same rules
	// as the name -> db mapping, so struct fields are lowercased and the `db` tag
	// is taken into consideration.
	rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)

	// batch insert

	// batch insert with structs
	personStructs := []Person{
		{FirstName: "Ardie", LastName: "Savea", Email: "asavea@ab.co.nz"},
		{FirstName: "Sonny Bill", LastName: "Williams", Email: "sbw@ab.co.nz"},
		{FirstName: "Ngani", LastName: "Laumape", Email: "nlaumape@ab.co.nz"},
	}

	_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
        VALUES (:first_name, :last_name, :email)`, personStructs)

	// batch insert with maps
	personMaps := []map[string]interface{}{
		{"first_name": "Ardie", "last_name": "Savea", "email": "asavea@ab.co.nz"},
		{"first_name": "Sonny Bill", "last_name": "Williams", "email": "sbw@ab.co.nz"},
		{"first_name": "Ngani", "last_name": "Laumape", "email": "nlaumape@ab.co.nz"},
	}

	_, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
        VALUES (:first_name, :last_name, :email)`, personMaps)
}

文章來源地址http://www.zghlxwxcb.cn/news/detail-652290.html

到了這里,關(guān)于Go學(xué)習(xí)第九天的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 100天精通Golang(基礎(chǔ)入門篇)——第5天: Go語言中的數(shù)據(jù)類型學(xué)習(xí)

    100天精通Golang(基礎(chǔ)入門篇)——第5天: Go語言中的數(shù)據(jù)類型學(xué)習(xí)

    ?? 博主 libin9iOak帶您 Go to Golang Language.? ?? 個人主頁——libin9iOak的博客?? ?? 《面試題大全》 文章圖文并茂??生動形象??簡單易學(xué)!歡迎大家來踩踩~?? ?? 《IDEA開發(fā)秘籍》學(xué)會IDEA常用操作,工作效率翻倍~?? ?? 希望本文能夠給您帶來一定的幫助??文章粗淺,敬請批

    2024年02月08日
    瀏覽(34)
  • 【go語言學(xué)習(xí)筆記】05 Go 語言實戰(zhàn)

    【go語言學(xué)習(xí)筆記】05 Go 語言實戰(zhàn)

    在做項目開發(fā)的時候,要善于借助已經(jīng)有的輪子,讓自己的開發(fā)更有效率,也更容易實現(xiàn)。 1. RESTful API 定義 RESTful API 是一套規(guī)范,它可以規(guī)范如何對服務(wù)器上的資源進(jìn)行操作。和 RESTful API 和密不可分的是 HTTP Method。 1.1 HTTP Method HTTP Method最常見的就是POST和GET,其實最早在

    2024年02月13日
    瀏覽(23)
  • 【Go】Go語言開發(fā)0基礎(chǔ)7天入門 - 筆記

    【Go】Go語言開發(fā)0基礎(chǔ)7天入門 - 筆記

    課程來源:【路飛學(xué)城】-黑金年卡VIP課程 課程名稱:GO語言開發(fā)0基礎(chǔ)7天入門 講師:【 前汽車之家架構(gòu)師 】Wusir-銀角大王 官網(wǎng):點擊進(jìn)入 集python簡潔 + C語言性能 詳情點擊 編程語言 實戰(zhàn)經(jīng)驗 源碼 并發(fā)架構(gòu) 新語言觸類旁通 1.1 開篇介紹(必看) 1.2 環(huán)境搭建前戲 1.3 mac系統(tǒng)G

    2024年02月16日
    瀏覽(30)
  • 6.Go語言學(xué)習(xí)筆記-結(jié)合chatGPT輔助學(xué)習(xí)Go語言底層原理

    6.Go語言學(xué)習(xí)筆記-結(jié)合chatGPT輔助學(xué)習(xí)Go語言底層原理

    1、Go版本 2、匯編基礎(chǔ) 推薦閱讀:GO匯編語言簡介 推薦閱讀:A Quick Guide to Go\\\'s Assembler - The Go Programming Language 精簡指令集 數(shù)據(jù)傳輸: MOV/LEA 跳轉(zhuǎn)指令: CMP/TEST/JMP/JCC 棧指令: PUSH/POP 函數(shù)調(diào)用指令: CALL/RET 算術(shù)指令: ADD/SUB/MUL/DIV 邏輯指令: AND/OR/XOR/NOT 移位指令: SHL/SHR JCC有條件跳轉(zhuǎn): JE

    2024年02月04日
    瀏覽(29)
  • 【go語言學(xué)習(xí)筆記】04 Go 語言工程管理

    【go語言學(xué)習(xí)筆記】04 Go 語言工程管理

    1. 單元測試 單元測試是保證代碼質(zhì)量的好方法,但單元測試也不是萬能的,使用它可以降低 Bug 率,但也不要完全依賴。除了單元測試外,還可以輔以 Code Review、人工測試等手段更好地保證代碼質(zhì)量。 1.1 定義 顧名思義,單元測試強(qiáng)調(diào)的是對單元進(jìn)行測試。在開發(fā)中,一個單

    2024年02月13日
    瀏覽(24)
  • Go語言學(xué)習(xí)筆記

    注:安裝教程 注:上一篇筆記 注:下一篇筆記 2.6、流程控制 2.6.1、條件語句 2.6.2、選擇語句 2.6.3、循環(huán)語句 2.6.4、跳轉(zhuǎn)語句 goto語句跳轉(zhuǎn)到本函數(shù)內(nèi)的某個標(biāo)簽 2.7、函數(shù) 2.7.1、函數(shù)定義 函數(shù)構(gòu)成代碼執(zhí)行的邏輯結(jié)構(gòu)。函數(shù)的基本組成為:func、函數(shù)名、參數(shù)列表、返回值

    2024年02月06日
    瀏覽(24)
  • 第四十九天學(xué)習(xí)記錄:C語言進(jìn)階:結(jié)構(gòu)體

    第四十九天學(xué)習(xí)記錄:C語言進(jìn)階:結(jié)構(gòu)體

    結(jié)構(gòu)體的聲明 結(jié)構(gòu)是一些值的集合,這些值稱為成員變量。結(jié)構(gòu)的每個成員可以是不同類型的變量 問:C++的new和C語言的結(jié)構(gòu)體有什么異同? ChatAI答: C++中的 new 是一個運(yùn)算符,用于在堆上分配動態(tài)內(nèi)存,并返回指向該內(nèi)存的地址。它會自動調(diào)用要分配的對象的構(gòu)造函數(shù),以

    2024年02月06日
    瀏覽(18)
  • go語言學(xué)習(xí)筆記1

    go語言學(xué)習(xí)筆記1

    ? GoLang是一種靜態(tài)強(qiáng)類型、編譯型、并發(fā)型,并具有 垃圾回收 功能的編程語言;它可以在不損失應(yīng)用程序性能的情況下極大的降低代碼的復(fù)雜性,還可以發(fā)揮多核處理器同步多工的優(yōu)點,并可解決面向?qū)ο蟪绦蛟O(shè)計的麻煩,并幫助程序設(shè)計師處理瑣碎但重要的內(nèi)存管理問題

    2024年02月12日
    瀏覽(18)
  • Go語言學(xué)習(xí)筆記(三)

    Go語言學(xué)習(xí)筆記(三)

    教程:文檔 - Go 編程語言 (studygolang.com) 在call-module-code需要注意,需要在hello目錄下操作 這是一個在Go項目的模塊管理中的命令。在Go的模塊管理工具( go mod )中,這個命令用于修改模塊依賴關(guān)系。 具體來說, go mod edit -replace example.com/greetings=../greetings ?這個命令的作用是:

    2024年02月02日
    瀏覽(14)
  • Go語言學(xué)習(xí)筆記(二)

    Go語言學(xué)習(xí)筆記(二)

    以下是一些推薦的Go語言學(xué)習(xí)資源的鏈接: Go語言教程:https://golang.org/doc/ Go by Example:Go by Example Golang Tutorials:https://golangtutorials.com/ Go語言第一課(慕課網(wǎng)):PHP模糊查詢技術(shù)案例視頻教程-慕課網(wǎng) Go語言進(jìn)階教程(實驗樓):極客企業(yè)版 Go語言高級編程(GitBook):誰是兇手

    2024年01月20日
    瀏覽(30)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包