<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                常量的定義與變量類似,只不過使用 const 關鍵字,代表永遠是只讀的,不能修改。 ~~~ const World = "世界" ~~~ 常量值必須是編譯期可確定的字符、字符串、布爾或數字類型的值。 ~~~ 常量不能使用 ":=" 語法定義。 ~~~ 數值常量 ~~~ 數值常量是高精度的值 。 ~~~ 一個未指定類型的常量由上下文來決定其類型。 ~~~ (int 可以存放最大64位的整數,根據平臺不同有時會更少。) ~~~ 常亮初始化: ~~~ package main const x, y int = 1, 2 // 多常量初始化 const s = "Hello, World!" // 類型推斷 const ( //常量組 a, b = 10, 100 c bool = false ) func main() { const str = "xxx" // 未使用的局部常量不會引發編譯錯誤。 } ~~~ 在常量組中,如不提供類型和初始化值,那么視作與上一個常量相同。 ~~~ package main import ( "fmt" ) const ( s = "abc" x // x = "abc" ) func main() { fmt.Println(s) fmt.Println(x) } ~~~ 輸出結果: ~~~ abc abc ~~~ 常量值還可以是 len、cap、unsafe.Sizeof 等編譯期可確定結果的函數返回值。 const ( a = "abc" b = len(a) c = unsafe.Sizeof(b) ) 如果常量類型足以存儲初始化值,那么不會引發溢出錯誤。 ~~~ package main import ( "fmt" "unsafe" ) const ( a = "hello world" b = len(a) c = unsafe.Sizeof(b) ) func main() { fmt.Println(a, b, c) } ~~~ 輸出結果: ~~~ hello world 11 8 ~~~ 枚舉 iota 可以被用作枚舉值: ~~~ iota,特殊常量,可以認為是一個可以被編譯器修改的常量。 在每一個const關鍵字出現時,被重置為0,然后再下一個const出現之前,每出現一次iota,其所代表的數字會自動增加1。 關鍵字 iota 定義常量組中從 0 開始按行計數的自增枚舉值。 ~~~ ~~~ package main import ( "fmt" ) const ( Sunday = iota Monday //通常省略后續行表達式 Tuesday Wednesday Thursday Friday Saturday ) func main() { fmt.Println(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday) } ~~~ 輸出結果: ~~~ 0 1 2 3 4 5 6 ~~~ ~~~ package main import ( "fmt" ) const ( _ = iota // iota = 0 KB int64 = 1 << (10 * iota) MB // iota=1 GB // 與 KB 表達式相同,但 iota = 2 TB ) func main() { fmt.Println(KB, MB, GB, TB) } ~~~ 輸出結果: ~~~ 1024 1048576 1073741824 1099511627776 ~~~ 在同一常量組中,可以提供多個 iota,它們各自增長。 ~~~ package main import ( "fmt" ) const ( A, B = iota, iota << 10 //0,0<<10 C, D // 1, 1 << 10 ) func main() { fmt.Println(A, B, C, D) } ~~~ 輸出結果: ~~~ 0 0 1 1024 ~~~ 如果 iota 自增被打斷,須顯式恢復。 ~~~ package main import ( "fmt" ) const ( A = iota //0 B // 1 C = "c" //c D // c,與上 相同。 E = iota // 4,顯式恢復。注意計數包含了 C、D 兩 。 F // 5 ) func main() { fmt.Println(A, B, C, D, E, F) } ~~~ 輸出結果: ~~~ 0 1 c c 4 5 ~~~ 可通過自定義類型來實現枚舉類型限制。 ~~~ package main type Color int const ( Black Color = iota Red Blue ) func test(c Color) {} func main() { c := Black test(c) // x := 1 // test(x) // ./main.go:18:6: cannot use x (type int) as type Color in argument to test test(1) // 常量會被編譯器自動轉換。 } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看