[go: up one dir, main page]

0% found this document useful (0 votes)
14 views1 page

Go Constants for Beginners

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Go Constants for Beginners

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Go by Example: Constants https://gobyexample.

com/constants

Go by Example: Constants
Go supports constants of character, string,
boolean, and numeric values.

package main

import (
"fmt"
"math"
)

const declares a constant value. const s string = "constant"

func main() {
fmt.Println(s)

A const statement can appear anywhere a var const n = 500000000


statement can.

Constant expressions perform arithmetic with const d = 3e20 / n


arbitrary precision. fmt.Println(d)

A numeric constant has no type until it’s given fmt.Println(int64(d))


one, such as by an explicit conversion.

A number can be given a type by using it in a fmt.Println(math.Sin(n))


context that requires one, such as a variable }
assignment or function call. For example, here
math.Sin expects a float64.

$ go run constant.go
constant
6e+11
600000000000
-0.28470407323754404

Next example: For.

by Mark McGranaghan and Eli Bendersky | source | license

1 of 1 11/26/24, 23:22

You might also like