Skip to content

Commit

Permalink
post: add go empty struct
Browse files Browse the repository at this point in the history
  • Loading branch information
donnol committed Sep 1, 2023
1 parent a8512a9 commit ec8f790
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions content/posts/2023/09/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
weight: 1
bookCollapseSection: true
title: "09"
---
45 changes: 45 additions & 0 deletions content/posts/2023/09/go_empty_struct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
author: "jdlau"
date: 2023-09-01
linktitle: Go Empty Struct
menu:
next:
prev:
title: Go Empty Struct
weight: 10
categories: ['Go']
tags: ['mark']
---

```go
package main

import (
"fmt"
"unsafe"
)

func main() {
type A struct{}
type B struct{}
// 结构体里的字段都是`Empty Struct`时,占用空间为0
type S struct {
A A
B B
}
var s S
fmt.Println(unsafe.Sizeof(s)) // prints 0
// 如果是指针,占用空间为8
fmt.Println(unsafe.Sizeof(&s)) // prints 8

var x [1000000000]struct{}
// 可以同时存储A和B类型元素
x[0] = A{}
x[1] = B{}
fmt.Println(unsafe.Sizeof(x)) // prints 0
// 地址一样
fmt.Printf("%p, %p", &x[0], &x[1]) // 0x54e3a0, 0x54e3a0
}
```

[See also](https://dave.cheney.net/2014/03/25/the-empty-struct)

0 comments on commit ec8f790

Please sign in to comment.