中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

php網(wǎng)頁(yè)設(shè)計(jì)論文淄博seo公司

php網(wǎng)頁(yè)設(shè)計(jì)論文,淄博seo公司,電商網(wǎng)站有哪些官網(wǎng),南寧網(wǎng)站建設(shè)智能優(yōu)化目錄標(biāo)題 一、Reflection反射1. What is reflection? 什么是反射2. Inspect a variable and find its type 檢查變量并找到它的類(lèi)型3. Reflect.Type and reflect.Value 反射類(lèi)型和值4. Reflect.Kind 查看底層種類(lèi)5. NumField() and Field() methods 字段數(shù)量和索引值方法6. In…

目錄標(biāo)題

  • 一、Reflection反射
    • 1. What is reflection? 什么是反射
    • 2. Inspect a variable and find its type 檢查變量并找到它的類(lèi)型
    • 3. Reflect.Type and reflect.Value 反射類(lèi)型和值
    • 4. Reflect.Kind 查看底層種類(lèi)
    • 5. NumField() and Field() methods 字段數(shù)量和索引值方法
    • 6. Int() and String() methods 整型和字符型方法
    • 7. Complete Program 完整示例
  • 二、 Reading Files
    • 1. Reading Files 讀取文件
    • 2. Using absolute file path 使用絕對(duì)路徑
    • 3. Passing the file path as a command line flag 將文件路徑作為命令行標(biāo)志傳遞
    • 4. Reading a file in small chunks
    • 5. Reading a file line by line 逐行讀取文件

一、Reflection反射

1. What is reflection? 什么是反射

	反射是Go中的高級(jí)主題之一,在Go語(yǔ)言中反射(reflection)是指在程序運(yùn)行時(shí)動(dòng)態(tài)地檢查類(lèi)型信息和操作對(duì)象的能力。通過(guò)反射,你可以在運(yùn)行時(shí)獲取類(lèi)型的信息,訪(fǎng)問(wèn)和修改對(duì)象的字段和方法,以及動(dòng)態(tài)地調(diào)用函數(shù)。 Go語(yǔ)言中的反射由reflect包提供支持。該包中的Type和Value類(lèi)型提供了訪(fǎng)問(wèn)和操作類(lèi)型和對(duì)象的方法。要使用反射,首先需要使用reflect.TypeOf()函數(shù)獲取一個(gè)值的類(lèi)型信息,或者使用reflect.ValueOf()函數(shù)獲取一個(gè)值的反射對(duì)象。這些函數(shù)返回的類(lèi)型對(duì)象或值對(duì)象包含了有關(guān)值的類(lèi)型、字段、方法等信息。反射對(duì)象的常用方法包括:Type.Kind():返回類(lèi)型的種類(lèi),如整數(shù)、字符串、結(jié)構(gòu)體等。Type.Name():返回類(lèi)型的名稱(chēng)。Type.Field(i int):返回結(jié)構(gòu)體類(lèi)型的第i個(gè)字段的反射對(duì)象。Type.NumField():返回結(jié)構(gòu)體類(lèi)型的字段數(shù)量。Value.Interface():將反射對(duì)象轉(zhuǎn)換為普通的接口類(lèi)型。Value.Kind():返回值的種類(lèi),如整數(shù)、字符串、結(jié)構(gòu)體等。Value.String():返回值的字符串表示。Value.Field(i int):返回結(jié)構(gòu)體類(lèi)型值的第i個(gè)字段的反射對(duì)象。Value.NumField():返回結(jié)構(gòu)體類(lèi)型值的字段數(shù)量。Value.Method(i int):返回值的第i個(gè)方法的反射對(duì)象。Value.Call(args []Value):調(diào)用值對(duì)應(yīng)的方法,傳遞參數(shù)并返回結(jié)果。通過(guò)使用這些方法,你可以在運(yùn)行時(shí)檢查和操作任意類(lèi)型的對(duì)象。例如,你可以獲取一個(gè)結(jié)構(gòu)體類(lèi)型的字段名和值,動(dòng)態(tài)調(diào)用函數(shù),或者創(chuàng)建新的對(duì)象。

2. Inspect a variable and find its type 檢查變量并找到它的類(lèi)型

        package mainimport ("fmt")type order struct {ordId      intcustomerId int}type employee struct {name    stringid      intaddress stringsalary  intcountry string}func createQuery(b order) string {i := fmt.Sprintf("insert into order values(%d, %d)", b.ordId, b.customerId)return i}func createQuerySet(b employee) string {i := fmt.Sprintf("insert into order values(%s, %d, %s, %d, %s)", b.name, b.id, b.address, b.salary, b.country)return i}func main() {a := 10fmt.Printf("%d, %T\n", a, a)b := order{171103,1006,}e := employee{"Like",1,"Shanghai",999999,"Minghang",}fmt.Println(createQuery(b))fmt.Println(createQuerySet(e))}// 10, int// insert into order values(171103, 1006)// insert into order values(Like, 1, Shanghai, 999999, Minghang)

3. Reflect.Type and reflect.Value 反射類(lèi)型和值

        package mainimport (  "fmt""reflect")type order struct {  ordId      intcustomerId int}func createQuery(q interface{}) {  t := reflect.TypeOf(q)v := reflect.ValueOf(q)fmt.Println("Type ", t)fmt.Println("Value ", v)}func main() {  o := order{ordId:      456,customerId: 56,}createQuery(o)}// Type  main.order  // Value  {456 56}  

4. Reflect.Kind 查看底層種類(lèi)

        package mainimport (  "fmt""reflect")type order struct {  ordId      intcustomerId int}func createQuery(q interface{}) {  t := reflect.TypeOf(q)k := t.Kind()fmt.Println("Type ", t)fmt.Println("Kind ", k)}func main() {  o := order{ordId:      456,customerId: 56,}createQuery(o)}// Type  main.order  // Kind  struct  

5. NumField() and Field() methods 字段數(shù)量和索引值方法

        package mainimport (  "fmt""reflect")type order struct {  ordId      intcustomerId int}func createQuery(q interface{}) {   if reflect.ValueOf(q).Kind() == reflect.Struct {	// struct == structv := reflect.ValueOf(q) 	// {456 56}fmt.Println("Number of fields", v.NumField())		// 2for i := 0; i < v.NumField(); i++ {fmt.Printf("Field:%d type:%T value:%v\n", i, v.Field(i), v.Field(i))}}}func main() {  o := order{ordId:      456,customerId: 56,}createQuery(o)}// Number of fields 2  // Field:0 type:reflect.Value value:456  // Field:1 type:reflect.Value value:56  

6. Int() and String() methods 整型和字符型方法

        package mainimport (  "fmt""reflect")func main() {  a := 56x := reflect.ValueOf(a).Int()fmt.Printf("type:%T value:%v\n", x, x)b := "Naveen"y := reflect.ValueOf(b).String()fmt.Printf("type:%T value:%v\n", y, y)}// type:int64 value:56  // type:string value:Naveen  

7. Complete Program 完整示例

        package mainimport (  "fmt""reflect")type order struct {  ordId      intcustomerId int}type employee struct {  name    stringid      intaddress stringsalary  intcountry string}func createQuery(q interface{}) {  if reflect.ValueOf(q).Kind() == reflect.Struct {t := reflect.TypeOf(q).Name()query := fmt.Sprintf("insert into %s values(", t)		// 輸出傳入的valuesv := reflect.ValueOf(q)		// 賦值vfor i := 0; i < v.NumField(); i++ {switch v.Field(i).Kind() {case reflect.Int:if i == 0 {query = fmt.Sprintf("%s%d", query, v.Field(i).Int())} else {query = fmt.Sprintf("%s, %d", query, v.Field(i).Int())}case reflect.String:if i == 0 {query = fmt.Sprintf("%s\"%s\"", query, v.Field(i).String())} else {query = fmt.Sprintf("%s, \"%s\"", query, v.Field(i).String())}default:fmt.Println("Unsupported type")return}}query = fmt.Sprintf("%s)", query)fmt.Println(query)return}fmt.Println("unsupported type")}func main() {  o := order{ordId:      456,customerId: 56,}createQuery(o)e := employee{name:    "Naveen",id:      565,address: "Coimbatore",salary:  90000,country: "India",}createQuery(e)i := 90createQuery(i)}//  insert into order values(456, 56)  //  insert into employee values("Naveen", 565, "Coimbatore", 90000, "India")  //  unsupported type  

二、 Reading Files

1. Reading Files 讀取文件

        package mainimport ("fmt""os")func main() {contents, err := os.ReadFile("test.txt")if err != nil {fmt.Println("File reading error", err)return}fmt.Println("Contents os file:", string(contents))}// Contents os file: Hello World. Welcome to file handling in GO

2. Using absolute file path 使用絕對(duì)路徑

        package mainimport ("fmt""os")func main() {contents, err := os.ReadFile("D:/Go/oop/test.txt")if err != nil {fmt.Println("File reading error", err)return}fmt.Println("Contents os file:", string(contents))}// Contents os file: Hello World. Welcome to file handling in GO

3. Passing the file path as a command line flag 將文件路徑作為命令行標(biāo)志傳遞

        package main  import (  "flag""fmt")func main() {  fptr := flag.String("fpath", "test.txt", "file path to read from")flag.Parse()contents, err := os.ReadFile(*fptr)if err != nil {fmt.Println("File reading error", err)return}fmt.Println("Contents of file: ", string(contents))}// Contents of file:  Hello World. Welcome to file handling in GO.

4. Reading a file in small chunks

        package mainimport ("bufio""flag""fmt""io""log""os")func main() {targetPath := flag.String("targetPath", "test.txt", "file path to read from")flag.Parse() // 進(jìn)行命令行參數(shù)的解析 將相應(yīng)的值賦給標(biāo)志變量targetPathf, err := os.Open(*targetPath) // err: nil  f: *os.Fileif err != nil {log.Fatal(err) // 將錯(cuò)誤信息打印到標(biāo)準(zhǔn)錯(cuò)誤輸出}defer func() { // 延遲開(kāi)啟匿名函數(shù)一直循環(huán) 如果關(guān)閉文件時(shí)發(fā)送錯(cuò)誤 log處理錯(cuò)誤if err = f.Close(); err != nil {log.Fatal(err)}}()r := bufio.NewReader(f) // 創(chuàng)建了一個(gè) bufio.Reader 對(duì)象r用于逐行讀取文件內(nèi)容b := make([]byte, 3)for {n, err := r.Read(b) //  r.Read(b) 方法讀取文件內(nèi)容 將讀取到的內(nèi)容存儲(chǔ)在字節(jié)切片b中 并返回讀取的字節(jié)數(shù)n和可能出現(xiàn)的錯(cuò)誤errif err == io.EOF {  // 如果讀取到文件末尾輸出fmt.Println("Finished reading file")break}if err != nil { // 如果讀取文件發(fā)生錯(cuò)誤fmt.Println("Error %s reading files", err)break}fmt.Println(string(b[0:n])) // 輸出切片內(nèi)容 切片容量3}}// Hel  // lo  // Wor  // ld.  //  We// lco  // me  // to  // fil  // e h  // and  // lin  // g i  // n G  // o.  // finished reading file  

5. Reading a file line by line 逐行讀取文件

        package mainimport (  "bufio""flag""fmt""log""os")func main() {  fptr := flag.String("fpath", "test.txt", "file path to read from")flag.Parse()f, err := os.Open(*fptr)if err != nil {log.Fatal(err)}defer func() {if err = f.Close(); err != nil {log.Fatal(err)}}()s := bufio.NewScanner(f)for s.Scan() {fmt.Println(s.Text())}err = s.Err()if err != nil {log.Fatal(err)}}// Hello World. Welcome to file handling in Go.  // This is the second line of the file.  // We have reached the end of the file.  
http://www.risenshineclean.com/news/4363.html

相關(guān)文章:

  • 蘇州最新通知天津搜索引擎優(yōu)化
  • 網(wǎng)站模板中心怎么投放廣告是最有效的
  • 優(yōu)設(shè)網(wǎng)站排行榜網(wǎng)站
  • 安徽網(wǎng)站建設(shè)哪家好免費(fèi)技能培訓(xùn)在哪里報(bào)名
  • 用ps網(wǎng)站首頁(yè)怎么做長(zhǎng)沙百度開(kāi)戶(hù)
  • 培訓(xùn)的網(wǎng)站建設(shè)互聯(lián)網(wǎng)營(yíng)銷(xiāo)師證書(shū)是國(guó)家認(rèn)可的嗎
  • 網(wǎng)站開(kāi)發(fā)模式acca少女網(wǎng)課視頻
  • 專(zhuān)做旅游酒店特價(jià)網(wǎng)站關(guān)鍵詞提取工具app
  • 關(guān)于政府網(wǎng)站建設(shè)的講話(huà)南寧網(wǎng)站快速排名提升
  • 中港建設(shè)集團(tuán)有限公司網(wǎng)站seo范疇
  • 網(wǎng)站建設(shè)中通知網(wǎng)上推廣平臺(tái)
  • 網(wǎng)站建設(shè)鼠標(biāo)滑動(dòng)效果網(wǎng)站優(yōu)化就是搜索引擎優(yōu)化
  • 合伙做網(wǎng)站怎么分配股權(quán)百度快速收錄接口
  • 中國(guó)建設(shè)網(wǎng)站首頁(yè)sem掃描電鏡是測(cè)什么的
  • 關(guān)于網(wǎng)站制作整站優(yōu)化工具
  • 個(gè)人做商城網(wǎng)站大概多少錢(qián)2021百度新算法優(yōu)化
  • 中學(xué)生網(wǎng)站作品免費(fèi)seo免費(fèi)培訓(xùn)
  • 校園文化建設(shè)網(wǎng)站素材西安排名seo公司
  • 免費(fèi)下載ppt模板網(wǎng)站哪個(gè)好免費(fèi)網(wǎng)站模板庫(kù)
  • 做充幣提現(xiàn)的網(wǎng)站優(yōu)化大師怎么卸載
  • 網(wǎng)站建設(shè)套餐價(jià)格已備案域名交易平臺(tái)
  • 51做網(wǎng)站廣州2022最近的新聞大事10條
  • 讓自己的電腦做網(wǎng)站的服務(wù)器營(yíng)銷(xiāo)軟文是什么意思
  • 珠海建站公司什么是搜索引擎優(yōu)化推廣
  • 網(wǎng)站系統(tǒng)下載不了文件百度代理服務(wù)器
  • 做的好的電商網(wǎng)站項(xiàng)目如何自己創(chuàng)建網(wǎng)站
  • 網(wǎng)站設(shè)計(jì)做哪些的百度軟件
  • 網(wǎng)站建設(shè)流程域名申請(qǐng)?jiān)扑阉飨螺d
  • 愛(ài)做網(wǎng)站免費(fèi)模板vip北京優(yōu)化網(wǎng)站公司
  • 網(wǎng)站建設(shè)創(chuàng)業(yè)基礎(chǔ)ppt模板電商seo是什么意思啊