Standard library / reflect
reflect.StructOf()
Construct a runtime struct type from field descriptors. Unsupported embedded methods and unexported fields can panic; use exported fields for portable construction. Expected stdout: '9\n'.
Formal Syntax & Structure
go syntaxgo
func StructOf(fields []StructField) TypeParameters & Attributes
| Name | Type | Description |
|---|---|---|
| fields | []StructField | Ordered field descriptors; names and types must satisfy StructOf restrictions. |
Return Value
Type
Construct a runtime struct type from field descriptors.
Working Example
Example Snippetgo
package main
import ( "fmt" "reflect")
func main() { t := reflect.StructOf([]reflect.StructField{{Name: "Count", Type: reflect.TypeOf(0)}}) v := reflect.New(t).Elem() v.Field(0).SetInt(9) fmt.Println(v.Field(0).Int())}