Standard library / go/types

go/types.NewConst()

Creates a constant declaration object with a type and exact constant value. Expected stdout: 'Answer 42\n'.

Formal Syntax & Structure

go syntaxgo
func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const

Parameters & Attributes

NameTypeDescription
postoken.PosSource position in the associated file set.
pkg*PackagePackage containing the symbol; nil for package-independent objects.
namestringName of the symbol or package.
typTypeType associated with the object or value.
valconstant.ValueExact constant value.

Return Value

*Const

Creates a constant declaration object with a type and exact constant value.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/constant"  "go/types")
func main() {  o := types.NewConst(0, nil, "Answer", types.Typ[types.Int], constant.MakeInt64(42))  fmt.Println(o.Name(), o.Val())}
Go Reference