亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    Go有幾種數(shù)據(jù)類(lèi)型

    Go有四種數(shù)據(jù)類(lèi)型:1、基礎(chǔ)類(lèi)型,包括整數(shù)、浮點(diǎn)數(shù)、復(fù)數(shù)、布爾值、字符串、常量;2、聚合類(lèi)型,包括數(shù)組、結(jié)構(gòu)體(一種聚合的數(shù)據(jù)類(lèi)型,是由零個(gè)或多個(gè)任意類(lèi)型的值聚合成的實(shí)體。每個(gè)值稱(chēng)為結(jié)構(gòu)體的成員);3、引用類(lèi)型,包括指針、slice、map、函數(shù)、通道;4、接口類(lèi)型,是對(duì)其它類(lèi)型行為的抽象和概括,是一種抽象的類(lèi)型。

    Go有幾種數(shù)據(jù)類(lèi)型

    本教程操作環(huán)境:windows7系統(tǒng)、GO 1.18版本、Dell G3電腦。

    Go的數(shù)據(jù)類(lèi)型一共分為四大類(lèi):基礎(chǔ)類(lèi)型、聚合類(lèi)型、引用類(lèi)型和接口類(lèi)型。

    • 基礎(chǔ)類(lèi)型分為:整數(shù)、浮點(diǎn)數(shù)、復(fù)數(shù)、布爾值、字符串、常量
    • 聚合類(lèi)型包括:數(shù)組、結(jié)構(gòu)體
    • 引用類(lèi)型包括:指針、slice、map、函數(shù)、通道
    • 接口類(lèi)型

    基礎(chǔ)類(lèi)型

    整數(shù):

    // int8 is the set of all signed 8-bit integers. // Range: -128 through 127. type int8 int8  // int16 is the set of all signed 16-bit integers. // Range: -32768 through 32767. type int16 int16  // int32 is the set of all signed 32-bit integers. // Range: -2147483648 through 2147483647. type int32 int32  // int64 is the set of all signed 64-bit integers. // Range: -9223372036854775808 through 9223372036854775807. type int64 int64  // uint8 is the set of all unsigned 8-bit integers. // Range: 0 through 255. type uint8 uint8  // uint16 is the set of all unsigned 16-bit integers. // Range: 0 through 65535. type uint16 uint16  // uint32 is the set of all unsigned 32-bit integers. // Range: 0 through 4294967295. type uint32 uint32  // uint64 is the set of all unsigned 64-bit integers. // Range: 0 through 18446744073709551615. type uint64 uint64  // byte is an alias for uint8 and is equivalent to uint8 in all ways. It is // used, by convention, to distinguish byte values from 8-bit unsigned // integer values. type byte = uint8  // rune is an alias for int32 and is equivalent to int32 in all ways. It is // used, by convention, to distinguish character values from integer values. type rune = int32  // int is a signed integer type that is at least 32 bits in size. It is a // distinct type, however, and not an alias for, say, int32. type int int  // uint is an unsigned integer type that is at least 32 bits in size. It is a // distinct type, however, and not an alias for, say, uint32. type uint uint
    登錄后復(fù)制

    浮點(diǎn)數(shù)

    // float32 is the set of all IEEE-754 32-bit floating-point numbers. type float32 float32  // float64 is the set of all IEEE-754 64-bit floating-point numbers. type float64 float64
    登錄后復(fù)制

    復(fù)數(shù)

    // complex64 is the set of all complex numbers with float32 real and // imaginary parts. type complex64 complex64  // complex128 is the set of all complex numbers with float64 real and // imaginary parts. type complex128 complex128  // The complex built-in function constructs a complex value from two // The complex built-in function constructs a complex value from two // floating-point values. The real and imaginary parts must be of the same // size, either float32 or float64 (or assignable to them), and the return // value will be the corresponding complex type (complex64 for float32, // complex128 for float64). func complex(r, i FloatType) ComplexType  // The real built-in function returns the real part of the complex number c. // The return value will be floating point type corresponding to the type of c. func real(c ComplexType) FloatType  // The imag built-in function returns the imaginary part of the complex // number c. The return value will be floating point type corresponding to // the type of c. func imag(c ComplexType) FloatType
    登錄后復(fù)制

    布爾值

    // bool is the set of boolean values, true and false. type bool bool  // true and false are the two untyped boolean values. const ( 	true  = 0 == 0 // Untyped bool. 	false = 0 != 0 // Untyped bool. )
    登錄后復(fù)制

    字符串

    // string is the set of all strings of 8-bit bytes, conventionally but not // necessarily representing UTF-8-encoded text. A string may be empty, but // not nil. Values of string type are immutable. type string string
    登錄后復(fù)制

    常量

    // iota is a predeclared identifier representing the untyped integer ordinal // number of the current const specification in a (usually parenthesized) // const declaration. It is zero-indexed. const iota = 0 // Untyped int.
    登錄后復(fù)制

    聚合類(lèi)型

    • 數(shù)組和結(jié)構(gòu)體是聚合類(lèi)型;
    • 它們的值由許多元素或成員字段的值組成。
    • 數(shù)組是由同構(gòu)的元素組成——每個(gè)數(shù)組元素都是完全相同的類(lèi)型——結(jié)構(gòu)體則是由異構(gòu)的元素組成的。
    • 數(shù)組和結(jié)構(gòu)體都是有固定內(nèi)存大小的數(shù)據(jù)結(jié)構(gòu)。
    • 相比之下,slice和map則是動(dòng)態(tài)的數(shù)據(jù)結(jié)構(gòu),它們將根據(jù)需要?jiǎng)討B(tài)增長(zhǎng)。

    數(shù)組

    a := [2]int{1, 2} b := [...]int{1, 2} c := [2]int{1, 3} fmt.Println(a == b, a == c, b == c) // "true false false" d := [3]int{1, 2} fmt.Println(a == d) // compile error: cannot compare [2]int == [3]int
    登錄后復(fù)制

    • 如果一個(gè)數(shù)組的元素類(lèi)型是可以相互比較的,那么數(shù)組類(lèi)型也是可以相互比較的,這時(shí)候我們可以直接通過(guò)==比較運(yùn)算符來(lái)比較兩個(gè)數(shù)組,只有當(dāng)兩個(gè)數(shù)組的所有元素都是相等的時(shí)候數(shù)組才是相等的。不相等比較運(yùn)算符!=遵循同樣的規(guī)則。
    • 當(dāng)調(diào)用一個(gè)函數(shù)的時(shí)候,函數(shù)的每個(gè)調(diào)用參數(shù)將會(huì)被賦值給函數(shù)內(nèi)部的參數(shù)變量,所以函數(shù)參數(shù)變量接收的是一個(gè)復(fù)制的副本,并不是原始調(diào)用的變量。因?yàn)楹瘮?shù)參數(shù)傳遞的機(jī)制導(dǎo)致傳遞大的數(shù)組類(lèi)型將是低效的,并且對(duì)數(shù)組參數(shù)的任何的修改都是發(fā)生在復(fù)制的數(shù)組上,并不能直接修改調(diào)用時(shí)原始的數(shù)組變量。Call by value值傳遞
    • 我們可以顯式地傳入一個(gè)數(shù)組指針,那樣的話函數(shù)通過(guò)指針對(duì)數(shù)組的任何修改都可以直接反饋到調(diào)用者

    結(jié)構(gòu)體

    • 結(jié)構(gòu)體是一種聚合的數(shù)據(jù)類(lèi)型,是由零個(gè)或多個(gè)任意類(lèi)型的值聚合成的實(shí)體。每個(gè)值稱(chēng)為結(jié)構(gòu)體的成員。
    • 如果結(jié)構(gòu)體成員名字是以大寫(xiě)字母開(kāi)頭的,那么該成員就是導(dǎo)出的;

    1, 結(jié)構(gòu)體值也可以用結(jié)構(gòu)體面值表示,結(jié)構(gòu)體面值可以指定每個(gè)成員的值。  type Point struct{ X, Y int } p := Point{1, 2}  2, 以成員名字和相應(yīng)的值來(lái)初始化,可以包含部分或全部的成員,  anim := gif.GIF{LoopCount: nframes}  在這種形式的結(jié)構(gòu)體面值寫(xiě)法中,如果成員被忽略的話將默認(rèn)用零值。  3, 因?yàn)榻Y(jié)構(gòu)體通常通過(guò)指針處理,可以用下面的寫(xiě)法來(lái)創(chuàng)建并初始化一個(gè)結(jié)構(gòu)體變量,并返回結(jié)構(gòu)體的地址:  pp := &Point{1, 2}  它是下面的語(yǔ)句是等價(jià)的  pp := new(Point) *pp = Point{1, 2}  不過(guò)&Point{1, 2}寫(xiě)法可以直接在表達(dá)式中使用,比如一個(gè)函數(shù)調(diào)用。
    登錄后復(fù)制

    • 如果結(jié)構(gòu)體的全部成員都是可以比較的,那么結(jié)構(gòu)體也是可以比較的,那樣的話兩個(gè)結(jié)構(gòu)體將可以使用或!=運(yùn)算符進(jìn)行比較。相等比較運(yùn)算符將比較兩個(gè)結(jié)構(gòu)體的每個(gè)成員,因此下面兩個(gè)比較的表達(dá)式是等價(jià)的:

    type Point struct{ X, Y int }  p := Point{1, 2} q := Point{2, 1} fmt.Println(p.X == q.X && p.Y == q.Y) // "false" fmt.Println(p == q)                   // "false"
    登錄后復(fù)制

    引用類(lèi)型

    指針

    一個(gè)指針變量指向了一個(gè)值的內(nèi)存地址。

    var ip *int        /* 指向整型*/  ip是一個(gè)指向int類(lèi)型對(duì)象的 指針 var fp *float32    /* 指向浮點(diǎn)型 */  fp是一個(gè)指向float32類(lèi)型對(duì)象的 指針
    登錄后復(fù)制

    指針使用流程:

    • 定義指針變量。
    • 為指針變量賦值。
    • 訪問(wèn)指針變量中指向地址的值。
      在指針類(lèi)型前面加上 * 號(hào)(前綴)來(lái)獲取指針?biāo)赶虻膬?nèi)容。

       var a int= 20   /* 聲明實(shí)際變量 */    var ip *int        /* 聲明指針變量 */     ip = &a  /* 指針變量的存儲(chǔ)地址 */     fmt.Printf("a 變量的地址是: %xn", &a  )     /* 指針變量的存儲(chǔ)地址 */    fmt.Printf("ip 變量?jī)?chǔ)存的指針地址: %xn", ip )     /* 使用指針訪問(wèn)值 */    fmt.Printf("*ip 變量的值: %dn", *ip )
    登錄后復(fù)制

    slice

    • Slice(切片)代表變長(zhǎng)的序列,序列中每個(gè)元素都有相同的類(lèi)型。
    • 一個(gè)slice類(lèi)型一般寫(xiě)作[]T,其中T代表slice中元素的類(lèi)型;
    • slice的語(yǔ)法和數(shù)組很像,只是沒(méi)有固定長(zhǎng)度而已

    type slice struct { 	array unsafe.Pointer 	len   int 	cap   int }
    登錄后復(fù)制

    • 多個(gè)slice可以復(fù)用同一個(gè)底層數(shù)組

    Go有幾種數(shù)據(jù)類(lèi)型

    // The len built-in function returns the length of v, according to its type: //	Array: the number of elements in v. //	Pointer to array: the number of elements in *v (even if v is nil). //	Slice, or map: the number of elements in v; if v is nil, len(v) is zero. //	String: the number of bytes in v. //	Channel: the number of elements queued (unread) in the channel buffer; //	         if v is nil, len(v) is zero. // For some arguments, such as a string literal or a simple array expression, the // result can be a constant. See the Go language specification's "Length and // capacity" section for details. func len(v Type) int  // The cap built-in function returns the capacity of v, according to its type: //	Array: the number of elements in v (same as len(v)). //	Pointer to array: the number of elements in *v (same as len(v)). //	Slice: the maximum length the slice can reach when resliced; //	if v is nil, cap(v) is zero. //	Channel: the channel buffer capacity, in units of elements; //	if v is nil, cap(v) is zero. // For some arguments, such as a simple array expression, the result can be a // constant. See the Go language specification's "Length and capacity" section for // details. func cap(v Type) int  // The append built-in function appends elements to the end of a slice. If // it has sufficient capacity, the destination is resliced to accommodate the // new elements. If it does not, a new underlying array will be allocated. // Append returns the updated slice. It is therefore necessary to store the // result of append, often in the variable holding the slice itself: //	slice = append(slice, elem1, elem2) //	slice = append(slice, anotherSlice...) // As a special case, it is legal to append a string to a byte slice, like this: //	slice = append([]byte("hello "), "world"...) func append(slice []Type, elems ...Type) []Type  // The make built-in function allocates and initializes an object of type // slice, map, or chan (only). Like new, the first argument is a type, not a // value. Unlike new, make's return type is the same as the type of its // argument, not a pointer to it. The specification of the result depends on // the type: //	Slice: The size specifies the length. The capacity of the slice is //	equal to its length. A second integer argument may be provided to //	specify a different capacity; it must be no smaller than the //	length. For example, make([]int, 0, 10) allocates an underlying array //	of size 10 and returns a slice of length 0 and capacity 10 that is //	backed by this underlying array. //	Map: An empty map is allocated with enough space to hold the //	specified number of elements. The size may be omitted, in which case //	a small starting size is allocated. //	Channel: The channel's buffer is initialized with the specified //	buffer capacity. If zero, or the size is omitted, the channel is //	unbuffered. func make(t Type, size ...IntegerType) Type  // The new built-in function allocates memory. The first argument is a type, // not a value, and the value returned is a pointer to a newly // allocated zero value of that type. func new(Type) *Type  // The copy built-in function copies elements from a source slice into a // destination slice. (As a special case, it also will copy bytes from a // string to a slice of bytes.) The source and destination may overlap. Copy // returns the number of elements copied, which will be the minimum of // len(src) and len(dst). func copy(dst, src []Type) int  // The delete built-in function deletes the element with the specified key // (m[key]) from the map. If m is nil or there is no such element, delete // is a no-op. func delete(m map[Type]Type1, key Type)
    登錄后復(fù)制

    map

    • 在Go語(yǔ)言中,一個(gè)map就是一個(gè)哈希表的引用,map類(lèi)型可以寫(xiě)為map[K]V,其中K和V分別對(duì)應(yīng)key和value。map中所有的key都有相同的類(lèi)型,所有的value也有著相同的類(lèi)型。
    • 其中K對(duì)應(yīng)的key必須是支持==比較運(yùn)算符的數(shù)據(jù)類(lèi)型,所以map可以通過(guò)測(cè)試key是否相等來(lái)判斷是否已經(jīng)存在。雖然浮點(diǎn)數(shù)類(lèi)型也是支持相等運(yùn)算符比較的,但是將浮點(diǎn)數(shù)用做key類(lèi)型則是一個(gè)壞的想法。
    • 對(duì)于V對(duì)應(yīng)的value數(shù)據(jù)類(lèi)型則沒(méi)有任何的限制。

    創(chuàng)建map: 1, 內(nèi)置的make函數(shù)可以創(chuàng)建一個(gè)map:  ages := make(map[string]int) // mapping from strings to ints  2, 我們也可以用map字面值的語(yǔ)法創(chuàng)建map,同時(shí)還可以指定一些最初的key/value:  ages := map[string]int{     "alice":   31,     "charlie": 34, } 這相當(dāng)于  ages := make(map[string]int) ages["alice"] = 31 ages["charlie"] = 34 因此,另一種創(chuàng)建空的map的表達(dá)式是map[string]int{}。  Map中的元素通過(guò)key對(duì)應(yīng)的下標(biāo)語(yǔ)法訪問(wèn): ages["alice"] = 32  delete(ages, "alice") // remove element ages["alice"]  所有這些操作是安全的,即使這些元素不在map中也沒(méi)有關(guān)系; 如果一個(gè)查找失敗將返回value類(lèi)型對(duì)應(yīng)的零值,例如, 即使map中不存在“bob”下面的代碼也可以正常工作,因?yàn)閍ges["bob"]失敗時(shí)將返回0。 ages["bob"] = ages["bob"] + 1 // happy birthday!  遍歷map  for name, age := range ages {     fmt.Printf("%st%dn", name, age) }
    登錄后復(fù)制

    函數(shù)

    函數(shù)聲明包括函數(shù)名、形式參數(shù)列表、返回值列表(可省略)以及函數(shù)體。

    func name(parameter-list) (result-list) {     body }
    登錄后復(fù)制

    channel 通道

    • 如果說(shuō)goroutine是Go語(yǔ)音程序的并發(fā)體的話,那么channels它們之間的通信機(jī)制。
    • 一個(gè)channels是一個(gè)通信機(jī)制,它可以讓一個(gè)goroutine通過(guò)它給另一個(gè)goroutine發(fā)送值信息。
    • 每個(gè)channel都有一個(gè)特殊的類(lèi)型,也就是channels可發(fā)送數(shù)據(jù)的類(lèi)型。一個(gè)可以發(fā)送int類(lèi)型數(shù)據(jù)的channel一般寫(xiě)為chan int。

    使用內(nèi)置的make函數(shù),我們可以創(chuàng)建一個(gè)channel:  使用內(nèi)置的make函數(shù),我們可以創(chuàng)建一個(gè)channel:  ch := make(chan int) // ch has type 'chan int'
    登錄后復(fù)制

    • 和map類(lèi)似,channel也一個(gè)對(duì)應(yīng)make創(chuàng)建的底層數(shù)據(jù)結(jié)構(gòu)的引用

    • 當(dāng)我們復(fù)制一個(gè)channel或用于函數(shù)參數(shù)傳遞時(shí),我們只是拷貝了一個(gè)channel引用,因此調(diào)用者何被調(diào)用者將引用同一個(gè)channel對(duì)象。和其它的引用類(lèi)型一樣,channel的零值也是nil。

    • 兩個(gè)相同類(lèi)型的channel可以使用==運(yùn)算符比較。如果兩個(gè)channel引用的是相通的對(duì)象,那么比較的結(jié)果為真。一個(gè)channel也可以和nil進(jìn)行比較。

    接口類(lèi)型

    接口

    • 接口類(lèi)型是對(duì)其它類(lèi)型行為的抽象和概括;因?yàn)榻涌陬?lèi)型不會(huì)和特定的實(shí)現(xiàn)細(xì)節(jié)綁定在一起,通過(guò)這種抽象的方式我們可以讓我們的函數(shù)更加靈活和更具有適應(yīng)能力。

    • 很多面向?qū)ο蟮恼Z(yǔ)言都有相似的接口概念,但Go語(yǔ)言中接口類(lèi)型的獨(dú)特之處在于它是滿(mǎn)足隱式實(shí)現(xiàn)的。

    • 也就是說(shuō),我們沒(méi)有必要對(duì)于給定的具體類(lèi)型定義所有滿(mǎn)足的接口類(lèi)型;簡(jiǎn)單地?fù)碛幸恍┍匦璧姆椒ň妥銐蛄恕?/p>

    • 這種設(shè)計(jì)可以讓你創(chuàng)建一個(gè)新的接口類(lèi)型滿(mǎn)足已經(jīng)存在的具體類(lèi)型卻不會(huì)去改變這些類(lèi)型的定義;當(dāng)我們使用的類(lèi)型來(lái)自于不受我們控制的包時(shí)這種設(shè)計(jì)尤其有用。

    • 接口類(lèi)型是一種抽象的類(lèi)型。它不會(huì)暴露出它所代表的對(duì)象的內(nèi)部值的結(jié)構(gòu)和這個(gè)對(duì)象支持的基礎(chǔ)操作的集合;它們只會(huì)展示出它們自己的方法。也就是說(shuō)當(dāng)你有看到一個(gè)接口類(lèi)型的值時(shí),你不知道它是什么,唯一知道的就是可以通過(guò)它的方法來(lái)做什么。

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)