Forked from kodelint/golang-struct-memory-allocation-optimized.go
Created
April 10, 2022 19:08
-
-
Save brainwire/aa9aaf1d230ca08a51856e3af7edef0b to your computer and use it in GitHub Desktop.
Golang Struct Memory Allocation Verifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "unsafe" | |
type TerraformResource struct { | |
Cloud string // 16 Bytes | |
Name string // 16 Bytes | |
PluginVersion string // 16 Bytes | |
TerraformVersion string // 16 Bytes | |
ModuleVersionMajor int32 // 4 Bytes | |
HaveDSL bool // 1 Byte | |
IsVersionControlled bool // 1 Byte | |
} | |
func main() { | |
var d TerraformResource | |
d.Cloud = "aws" | |
d.Name = "ec2" | |
d.HaveDSL = true | |
d.PluginVersion = "3.64" | |
d.TerraformVersion = "1.1" | |
d.ModuleVersionMajor = 1 | |
d.IsVersionControlled = true | |
fmt.Println("==============================================================") | |
fmt.Printf("Total Memory Usage StructType:d %T => [%d]\n", d, unsafe.Sizeof(d)) | |
fmt.Println("==============================================================") | |
fmt.Printf("Cloud Field StructType:d.Cloud %T => [%d]\n", d.Cloud, unsafe.Sizeof(d.Cloud)) | |
fmt.Printf("Name Field StructType:d.Name %T => [%d]\n", d.Name, unsafe.Sizeof(d.Name)) | |
fmt.Printf("HaveDSL Field StructType:d.HaveDSL %T => [%d]\n", d.HaveDSL, unsafe.Sizeof(d.HaveDSL)) | |
fmt.Printf("PluginVersion Field StructType:d.PluginVersion %T => [%d]\n", d.PluginVersion, unsafe.Sizeof(d.PluginVersion)) | |
fmt.Printf("ModuleVersionMajor Field StructType:d.IsVersionControlled %T => [%d]\n", d.IsVersionControlled, unsafe.Sizeof(d.IsVersionControlled)) | |
fmt.Printf("TerraformVersion Field StructType:d.TerraformVersion %T => [%d]\n", d.TerraformVersion, unsafe.Sizeof(d.TerraformVersion)) | |
fmt.Printf("ModuleVersionMajor Field StructType:d.ModuleVersionMajor %T => [%d]\n", d.ModuleVersionMajor, unsafe.Sizeof(d.ModuleVersionMajor)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment