成人怡红院-成人怡红院视频在线观看-成人影视大全-成人影院203nnxyz-美女毛片在线看-美女免费黄

站長資訊網
最全最豐富的資訊網站

Go有幾種數據類型

Go有四種數據類型:1、基礎類型,包括整數、浮點數、復數、布爾值、字符串、常量;2、聚合類型,包括數組、結構體(一種聚合的數據類型,是由零個或多個任意類型的值聚合成的實體。每個值稱為結構體的成員);3、引用類型,包括指針、slice、map、函數、通道;4、接口類型,是對其它類型行為的抽象和概括,是一種抽象的類型。

Go有幾種數據類型

本教程操作環境:windows7系統、GO 1.18版本、Dell G3電腦。

Go的數據類型一共分為四大類:基礎類型、聚合類型、引用類型和接口類型。

  • 基礎類型分為:整數、浮點數、復數、布爾值、字符串、常量
  • 聚合類型包括:數組、結構體
  • 引用類型包括:指針、slice、map、函數、通道
  • 接口類型

基礎類型

整數:

// 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
登錄后復制

浮點數

// 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
登錄后復制

復數

// 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
登錄后復制

布爾值

// 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. )
登錄后復制

字符串

// 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
登錄后復制

常量

// 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.
登錄后復制

聚合類型

  • 數組和結構體是聚合類型;
  • 它們的值由許多元素或成員字段的值組成。
  • 數組是由同構的元素組成——每個數組元素都是完全相同的類型——結構體則是由異構的元素組成的。
  • 數組和結構體都是有固定內存大小的數據結構。
  • 相比之下,slice和map則是動態的數據結構,它們將根據需要動態增長。

數組

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
登錄后復制

  • 如果一個數組的元素類型是可以相互比較的,那么數組類型也是可以相互比較的,這時候我們可以直接通過==比較運算符來比較兩個數組,只有當兩個數組的所有元素都是相等的時候數組才是相等的。不相等比較運算符!=遵循同樣的規則。
  • 當調用一個函數的時候,函數的每個調用參數將會被賦值給函數內部的參數變量,所以函數參數變量接收的是一個復制的副本,并不是原始調用的變量。因為函數參數傳遞的機制導致傳遞大的數組類型將是低效的,并且對數組參數的任何的修改都是發生在復制的數組上,并不能直接修改調用時原始的數組變量。Call by value值傳遞
  • 我們可以顯式地傳入一個數組指針,那樣的話函數通過指針對數組的任何修改都可以直接反饋到調用者

結構體

  • 結構體是一種聚合的數據類型,是由零個或多個任意類型的值聚合成的實體。每個值稱為結構體的成員。
  • 如果結構體成員名字是以大寫字母開頭的,那么該成員就是導出的;

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

  • 如果結構體的全部成員都是可以比較的,那么結構體也是可以比較的,那樣的話兩個結構體將可以使用或!=運算符進行比較。相等比較運算符將比較兩個結構體的每個成員,因此下面兩個比較的表達式是等價的:

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"
登錄后復制

引用類型

指針

一個指針變量指向了一個值的內存地址。

var ip *int        /* 指向整型*/  ip是一個指向int類型對象的 指針 var fp *float32    /* 指向浮點型 */  fp是一個指向float32類型對象的 指針
登錄后復制

指針使用流程:

  • 定義指針變量。
  • 為指針變量賦值。
  • 訪問指針變量中指向地址的值。
    在指針類型前面加上 * 號(前綴)來獲取指針所指向的內容。

   var a int= 20   /* 聲明實際變量 */    var ip *int        /* 聲明指針變量 */     ip = &a  /* 指針變量的存儲地址 */     fmt.Printf("a 變量的地址是: %xn", &a  )     /* 指針變量的存儲地址 */    fmt.Printf("ip 變量儲存的指針地址: %xn", ip )     /* 使用指針訪問值 */    fmt.Printf("*ip 變量的值: %dn", *ip )
登錄后復制

slice

  • Slice(切片)代表變長的序列,序列中每個元素都有相同的類型。
  • 一個slice類型一般寫作[]T,其中T代表slice中元素的類型;
  • slice的語法和數組很像,只是沒有固定長度而已

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

  • 多個slice可以復用同一個底層數組

Go有幾種數據類型

// 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)
登錄后復制

map

  • 在Go語言中,一個map就是一個哈希表的引用,map類型可以寫為map[K]V,其中K和V分別對應key和value。map中所有的key都有相同的類型,所有的value也有著相同的類型。
  • 其中K對應的key必須是支持==比較運算符的數據類型,所以map可以通過測試key是否相等來判斷是否已經存在。雖然浮點數類型也是支持相等運算符比較的,但是將浮點數用做key類型則是一個壞的想法。
  • 對于V對應的value數據類型則沒有任何的限制。

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

函數

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

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

channel 通道

  • 如果說goroutine是Go語音程序的并發體的話,那么channels它們之間的通信機制。
  • 一個channels是一個通信機制,它可以讓一個goroutine通過它給另一個goroutine發送值信息。
  • 每個channel都有一個特殊的類型,也就是channels可發送數據的類型。一個可以發送int類型數據的channel一般寫為chan int。

使用內置的make函數,我們可以創建一個channel:  使用內置的make函數,我們可以創建一個channel:  ch := make(chan int) // ch has type 'chan int'
登錄后復制

  • 和map類似,channel也一個對應make創建的底層數據結構的引用

  • 當我們復制一個channel或用于函數參數傳遞時,我們只是拷貝了一個channel引用,因此調用者何被調用者將引用同一個channel對象。和其它的引用類型一樣,channel的零值也是nil。

  • 兩個相同類型的channel可以使用==運算符比較。如果兩個channel引用的是相通的對象,那么比較的結果為真。一個channel也可以和nil進行比較。

接口類型

接口

  • 接口類型是對其它類型行為的抽象和概括;因為接口類型不會和特定的實現細節綁定在一起,通過這種抽象的方式我們可以讓我們的函數更加靈活和更具有適應能力。

  • 很多面向對象的語言都有相似的接口概念,但Go語言中接口類型的獨特之處在于它是滿足隱式實現的。

  • 也就是說,我們沒有必要對于給定的具體類型定義所有滿足的接口類型;簡單地擁有一些必需的方法就足夠了。

  • 這種設計可以讓你創建一個新的接口類型滿足已經存在的具體類型卻不會去改變這些類型的定義;當我們使用的類型來自于不受我們控制的包時這種設計尤其有用。

  • 接口類型是一種抽象的類型。它不會暴露出它所代表的對象的內部值的結構和這個對象支持的基礎操作的集合;它們只會展示出它們自己的方法。也就是說當你有看到一個接口類型的值時,你不知道它是什么,唯一知道的就是可以通過它的方法來做什么。

贊(0)
分享到: 更多 (0)
?
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
波多野结衣av电影在线观看| 国产精品一区二区 尿失禁| 全黄H全肉边做边吃奶| A在线视频播放观看免费观看| 日本牲交大片免费观看| 国产精品久久久久精品日日| 久久久久久AV无码免费网站| 亚洲日韩国产精品无码AV| 女特警被三四个黑人糟蹋| 国产高清一区二区三区视频 | 97人人模人人爽人人少妇| 日本高清中文字幕在线观穿线视频 | 日本适合十八岁以上人群的护肤品| 国产福利一区二区久久| 亚洲一区二区三区在线播放无码| 欧洲精品久久久AV无码电影| 国产精品亚洲精品日韩已方| 中文字幕无码一区二区黑人巨大| 香蕉久久AV一区二区三区APP| 欧美丰满性久久久久久久| 国产精品久久久久无码AV| 中国亲子伦孑XXⅩ| 午夜不卡AV免费| 欧美激欧美啪啪片免费看| 娇小XXXXBXBⅨ中国XX| 熟女内射婷婷直播| 国内精品久久人妻无码不卡| 被黑人下药做得受不了| 曰韩欧美群交P片内射| 五月综合激情婷婷六月色窝| 欧式春画图片大全欣赏简单| 国内精品视频一区二区三区 | 老公带朋友来家里C我怎么办| 国产精华精华液一二三区别| 18禁爆乳无遮挡免费观看日本动| 无码天堂亚洲国产AV久久| 日欧 片内射AV在线影院| 欧美丰满熟妇XXXX| 老熟女露脸内射正在播放| 国产熟妇XXXXXⅩ性Ⅹ交| 国产Chinese男男做受g片| ASS鲜嫩鲜嫩PICS日本| 一本久道综合在线无码88| 亚洲精品无码专区在线在线播放| 亚洲妇熟XXXX妇色黄无码| 亚洲AV无码成人网站国产网站| 久久精品无码一区二区无码| 国产成人啪精品视频免费APP | 亚洲制服丝袜中文字幕在线| 亚洲 校园 欧美 国产 另类| 无码专区HEYZO色欲AV| 少女たちよ在线观看动漫在线观看| 麻豆成人传媒一区二区| 久久久久无码精品国产AV蜜桃| 黑人巨大跨种族VIDEO| 国偷自产一区二区免费视频| 国产日产欧产精品精品| 大象国精产品一品二品在线| 成人网站国产在线视频内射视频| 嗯~别停~用力点~再快点| 国产成人无码AA精品一区 | 老师的粉嫩小又紧水又多| 久久精品日日躁夜夜躁欧美| 久久久久久久久精品中文字幕| 妺妺自愿做我的性玩具| 欧美XXXX做受欧美GAY| 人妻丰满熟妇av无码区免费蜜臀| 男男GAY无套国产| 欧美精产国品一二三类产品特点| 女人18片毛片60分钟中国| 欧美激情性XXXXX高清真| 色婷婷五月综合亚洲影院| 无码一区二区三区蜜桃| 亚洲午夜久久久影院伊人| 曰批免费视频免费无码软件| 2021最新国产在线人成| 班级每人C了我半小时班长| 最新亚洲人成无码网WWW电影| 成熟老年妇女毛茸茸| 国内揄拍高清国内精品对白 | 麻豆一区二区三区精品视频| 欧美高潮抽搐喷水大叫| 丝袜国偷自产中文字幕| 午夜麻豆国产精品无码| 在线观看成人无码中文AV天堂不| 肥胖BMGBMGBMG多毛图片| 国产在线拍揄自揄视频网站| 久久人妻AV无码中文专区 | 压在窗户上C给别人看窗前| 亚洲成AV人片天堂网无码| AV 日韩 人妻 黑人 综合| 国产精品久久久久久亚洲AV| 久久久久亚洲AV无码专区喷水 | 内射爆草少妇精品视频| 人人模人人爽人人喊久久| 亚洲AV无码丰满尖叫高潮| AV优选天堂污污污成人亚洲 | 日韩AV无码中文无码不卡电影 | 精品人妻一区二区三区免费看 | 透过校服的乳尖 揉捏| 中国高清WINDOWS视频软件| 国产精品成人一区无码 | 亚洲 欧美精品SUV| ZOZ○ZO女人和另类ZOZ0| 狠狠色噜噜狠狠狠狠7777米奇| 日本高清视频www| 曰本女人牲交视频视频免费 | 国产白丝护士AV在线网站| 欧美乱妇日本无乱码特黄大片| 性色AV性色生活片| 赤裸羔羊Ⅲ致命快感 电影| 免费无码作爱视频| 亚洲一成人精品无码一区二区三区| 国产操熟女性爱导航| 欧美成人一区二区三区不卡| 亚洲国产成人精品无码区在线播放 | 蜜臀av免费一区二区三区观看 | JIZZJIZZ中国护士高清多| 久99久热爱视频精品免费37| 天堂影院一区二区三区四区 | 久久精品国产亚洲AV香蕉| 亚洲精品无码不卡AV| JLZZ大全高潮多水| 特级毛片AAAAAA| 荡公乱妇第1章95| 日韩精品一区二区视频| 喑交小拗女一区二区三区| 韩国精品福利一区二区三区| 无码国产成人午夜电影在线观看| 在线无码午夜福利高潮视频| 国产人妻麻豆蜜桃色精品电影| 无码人妻久久1区2区3区| 国产精品欧美一区二区三区| 无码精品人妻 中文字幕| 国产婷婷丁香五月缴情成人网| 日本护士OOXⅩXXHD| 把腿张开老子臊烂你的漫画 | 亚洲国产成人久久综合| 精品人妻少妇一区| 亚洲AV无码专区亚洲AV桃| 国产区在线观看成人精品| 亚洲A∨无码一区二区| 国产亚洲精品自在久久| 午夜无码人妻AV大片色欲 | 去掉小内打扑克的视频| 成人片黄网站色大片免费观看CN| 欧美裸体XXXX极品少妇| 被C了一节课的林妙妙| 欧美乱熟妇XXXX白浆| 粗大的内捧猛烈进出动态图| 人妻蜜と1~4中文字幕月野定规| 2019理论韩国理论中文| 日本夜爽爽一区二区三区| 国产精品国产三级国产专不| 天天狠天天透天干天天怕∴| 国产乱子夫妻XX黑人XYX真爽| 国产精品无码专区AV在线播放| 天堂BT种子资源在线WWW| 国产成人V在线免播放观看| 亚洲精品国产成人AV蜜臀| 女人张开腿让男人桶爽的| 99精品久久久久精品双飞| 无码人妻丰满熟妇惹区| 老熟妇乱子伦牲交视频| 高潮到不停喷水的免费视频| 午夜精品一区二区三区在线视| 久久亚洲精品人成综合网| 国产精品亚洲VA在线| 中文天堂资源在线WWW| 性一交一乱一伦一色一情孩交| 欧美一级 片内射欧美A999| 久久国产精品香蕉成人APP| 国产CHINESEHDXXXX宾馆TUBE| 99国产欧美久久久精品蜜芽| 亚洲精品自产拍在线观看 | 国产94在线 | 亚洲| ASS黑森林PIC| 中文字幕无码一线二线三线| 亚洲国产AⅤ精品一区二区蜜桃| 天美传媒免费观看一二三在线| 全免费A级毛片免费看网站| 免费无码午夜福利电影网| 丰满的人妻HD高清日本| 国产亚洲AV人片在线观看| 无码熟妇人妻AV在线C0930| 女人什么姿势下面最紧| 精品无码人妻被多人侵犯aⅴ| 妇女性内射冈站HDWWWOOO| 99无码精品二区在线视频| 一本久道久久综合狠狠老| 亚洲AV永久中文无码精品综合| 无码成人AAAAA毛片| 日韩精品无码专区免费播放 | 亚洲AV无码精品网站| 日产乱码一二三区别免费演员表 | 无码免费大香伊蕉在人线国产 | 成人无码一区二区三区| 9人妻人人澡人人爽人人精品| 亚洲中文在线精品国产| 亚洲国产精品无码久久电影|