pandascope
pandascope

Reputation: 147

does golang compiler use constant folding?

just wondering if "go" compiler uses any sort of optimization such as constant folding.

https://en.wikipedia.org/wiki/Constant_folding

searched through google but couldn't find the answer i'm looking for.

Upvotes: 3

Views: 741

Answers (2)

peterSO
peterSO

Reputation: 166785

Constant folding - Wikipedia

Constant folding is the process of recognizing and evaluating constant expressions at compile time rather than computing them at runtime.


The Go Programming Language Specification

Constant expressions

Constant expressions may contain only constant operands and are evaluated at compile time.


Package big

import "math/big"

Package big implements arbitrary-precision arithmetic (big numbers).


Go constant expressions are evaluated at compile time. The Go gc compiler, written in Go, uses package big to evaluate numeric constant expressions.

Upvotes: 8

lofcek
lofcek

Reputation: 1233

Try to write a simple program: eg.

package main

import "fmt"

func main() {
    fmt.Println(12345*1000)
}

And now compile it to assembly

go tool compile -S examle.go

And now find 12345 in the result and you will have an answer.

Upvotes: 1

Related Questions