Standard library / go/types

go/types.AssignableTo()

Reports whether a value of the first type can be assigned to the second type. Type-only checks do not test constant representability. Expected stdout: 'true false\n'.

Formal Syntax & Structure

go syntaxgo
func AssignableTo(V, T Type) bool

Parameters & Attributes

NameTypeDescription
VTypeSource or candidate type.
TTypeDestination type or interface, or type to inspect.

Return Value

bool

Reports whether a value of the first type can be assigned to the second type.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/types")
func main() {  fmt.Println(types.AssignableTo(types.Typ[types.Int], types.Typ[types.Int]), types.AssignableTo(types.Typ[types.Int], types.Typ[types.String]))}
Go Reference