exprvals

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 4 Imported by: 0

README

Exprvals - Determine the possible values for a Go expression

Go Reference Go Report Card Tests Coverage Status

This is exprvals, a library for inspecting a Go expression and its surrounding code in order to determine the possible values it may hold.

Documentation

Overview

Package exprvals provides a way to scan Go AST expressions for the values they can represent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Scan

func Scan(node ast.Expr, files []*ast.File, info *types.Info) (map[string]constant.Value, bool)

Scan scans the given AST expression node to determine the values it might represent. If the node is a boolean, string, or number literal, that is its value. If it is an identifier that refers to a constant, Scan returns that value. If it is an identifier that refers to a variable, Scan looks at all the assignments to that variable to determine the possible values. In the future, other types of expression may be supported.

The result is a map of [constant.Value]s. Each key is the string representation (using ExactString) of the value. This function also returns a boolean indicating whether all possible values were determined.

For example, given the following code:

x := "hello"
if condition() {
  x = "goodbye"
}
return x

Scan can determine that, by the time the return statement is reached, x can be only "hello" or "goodbye" and nothing else.

func ScanCallResult added in v0.2.0

func ScanCallResult(call *ast.CallExpr, idx int, files []*ast.File, info *types.Info) (map[string]constant.Value, bool)

ScanCallResult performs a Scan on the idx'th result of the given call expression.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL