package main import ( "fmt" "reflect" ) type Person struct { Name string Age int } func main() { p := Person{Name: "Alice", Age: 30} size := reflect.TypeOf(p).Size() fmt.Printf("The size of the Person struct is %d bytes.\n", size) }
package main import ( "fmt" "reflect" ) func main() { i := 42 size := reflect.TypeOf(i).Size() fmt.Printf("The size of int is %d bytes.\n", size) }In this example, a variable `i` of type `int` is created and assigned the value 42. The size of the `int` type is then determined using the `reflect` package's `TypeOf` function to get the type of `i` and the `Size` method to get its size. The result is printed to the console. This example shows that the size of a built-in type can also be determined using reflect. Package library: reflect.