Go by Example: Constants

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Go語言支援 字元、字串、布爾型和數字類型變數的常量。

package mainimport "fmt"import "math"//使用關鍵詞const聲明一個常量const s string = "constant"func main() {    fmt.Println(s)        // 任意一個var聲明可以出現地方都可以使用const聲明    const n = 500000000    //常量運算式可以執行任意精度的計算。    const d = 3e20 / n    fmt.Println(d)    //一個數值常量沒有類型,除非語句中給出相應類型,例如使用強制類型轉換    fmt.Println(int64(d))        //根據語句的上下文,一個數值常量會被賦予相應的類型。例如:在變數聲明或者函數調用中    //例如,這裡函數math.Sin()需要一個float64的數值,則n會當作float64數值進行操作。    fmt.Println(math.Sin(n))}
輸出

$ go run constant.go constant6e+11600000000000-0.28470407323754404


下一個例子:For。


英文原文


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.