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) *ConstParameters & Attributes
| Name | Type | Description |
|---|---|---|
| pos | token.Pos | Source position in the associated file set. |
| pkg | *Package | Package containing the symbol; nil for package-independent objects. |
| name | string | Name of the symbol or package. |
| typ | Type | Type associated with the object or value. |
| val | constant.Value | Exact 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())}