4.Go by Example:constants

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Go by Example:constants

Go supports constants of character, String, Boolean, and numeric values.

Const declares a constant value.

A Const statement can appear anywhere a var statement can.

Constant expressions perform arithmetic with arbitrary precision.

A numeric constant has no type until it's given one, such as by an explicit cast.


A number can be given a type by using it in a context the requires one, such as a variable assignment or function call. For example, here Math. Sin expects a float64.


Package Mainimport "FMT" import "math" Const s string = "constant" Func main () {    fmt. Println (s)     const n = 500000000     Const D = 3e20/n    fmt. Println (d)     FMT. PRINTLN (Int64 (d))    FMT. Println (Math. Sin (n))}


Translated:
The Go language supports constant types with Characer string character Boolean numeric numeric value


Declare a constant value by using the Const keyword


A constant declaration can show that any VAR declaration can be

Constant expressions can show arbitrary precision arithmetic operations


An arithmetic expression can have no type until he is given a value, for example by displaying a transform


A number can be used when it is necessary to use a given type, such as the assignment of a variable or a call to a function.

For example, here's math. Sin is expected to be a float64 type.

Package Mainimport "FMT" import "math" Const s string = "constant" Func main () {    fmt. Println (s)     const n = 500000000//Define a constant note here the constant type is not allowed to change the         const D = 3e20/n   //The FMT is computed according to the science and technology method    . Println (d)     FMT. PRINTLN (Int64 (d))    FMT. Println (Math. Sin (n))}

Run results
$ go Run constant.go
constant
6e+11
600000000000
-0.28470407323754404

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.