Standard library / reflect

reflect.Value.CanConvert()

Check whether this particular value can be converted to the target type without panic. This includes length-dependent slice-to-array conversions. Expected stdout: 'false\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) CanConvert(t Type) bool

Parameters & Attributes

NameTypeDescription
tTypeTarget or element type, as specified by the operation.

Return Value

bool

Check whether this particular value can be converted to the target type without panic.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() { v := reflect.ValueOf([]int{1}); fmt.Println(v.CanConvert(reflect.TypeOf([2]int{}))) }
Go Reference