-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
weight: 1 | ||
bookCollapseSection: true | ||
title: "09" | ||
--- |
This file contains 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
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) |