英文:
Equivalent of sizeof(aType) in Go
问题
在Go语言中,没有与C++中的sizeof
函数完全等效的函数。Go语言中的类型大小是在编译时确定的,并且可以使用unsafe.Sizeof
函数来获取某个类型的大小。unsafe.Sizeof
函数返回的是该类型所占用的字节数,但需要注意的是,它并不考虑内存对齐的情况。因此,在使用unsafe.Sizeof
函数时需要谨慎,尤其是对于包含指针或其他非基本类型的结构体。
英文:
C++ and several other languages have a function called sizeof(int)
(or whatever type you need) that returns the number of bytes consumed by a particular data type, in the current system.
Is there an equivalent function in Go? What is it?
答案1
得分: 16
如果您想要找出特定值的大小,有两种方法可以实现 - 使用unsafe包或使用reflection包。以下代码演示了两种方法:
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i int
fmt.Printf("变量的大小(reflect.TypeOf.Size):%d\n", reflect.TypeOf(i).Size())
fmt.Printf("变量的大小(unsafe.Sizeof):%d\n", unsafe.Sizeof(i))
}
然而,我不知道直接获取类型大小的方法。但我认为您会发现,在Go语言中不像C语言那样经常需要使用sizeof函数。
英文:
If you want to find out the size of a particular value, there are two ways to do that - using the unsafe package, or using the reflection package. The following code demonstrates both:
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
var i int
fmt.Printf("Size of var (reflect.TypeOf.Size): %d\n", reflect.TypeOf(i).Size())
fmt.Printf("Size of var (unsafe.Sizeof): %d\n", unsafe.Sizeof(i))
}
However, I am not aware of a way to get the size of a type directly. But I think you'll find out that the sizeof function is not needed as often as in C.
答案2
得分: 13
在Go语言中,sizeof
的等价物是unsafe.Sizeof
。它与C语言中的sizeof
有一个区别,就是它只定义在值上(而在C语言中,它适用于值和类型)。另一个主要区别是,在Go语言中几乎不需要使用它,而在C语言中它相当常见。
一个示例是:
package main
import (
"fmt"
"unsafe"
)
func main() {
fmt.Println(unsafe.Sizeof(int(0)))
}
英文:
The equivalent of sizeof
in go is unsafe.Sizeof
. One difference between it and sizeof in C is that it's only defined on values (whereas in C, it works for values and types). The other main difference is that in go it's hardly ever needed, whereas in C it's fairly common.
An example is:
package main
import (
"fmt"
"unsafe"
)
func main() {
fmt.Println(unsafe.Sizeof(int(0)))
}
答案3
得分: 10
如果你只是想找出int
或uint
的大小,可以使用strconv.IntSize
。
例如,
package main
import (
"fmt"
"runtime"
"strconv"
)
func main() {
fmt.Println(runtime.Compiler, runtime.GOARCH, runtime.GOOS)
fmt.Println(strconv.IntSize)
}
输出:
gc amd64 linux
64
英文:
If you simply want to find out the size of an int
or uint
, use strconv.IntSize
.
> Package strconv
>
> Constants
>
> const IntSize = intSize
>
> IntSize
is the size in bits of an int
or uint
value.
For example,
package main
import (
"fmt"
"runtime"
"strconv"
)
func main() {
fmt.Println(runtime.Compiler, runtime.GOARCH, runtime.GOOS)
fmt.Println(strconv.IntSize)
}
Output:
gc amd64 linux
64
答案4
得分: 4
不是真正的答案,而是原始问题后面的评论线程的延续...
要实现一个内存中的数据集,我会采用以下方法之一:
-
(简单方法):只需使用
interface{}
的数组来存储行。每个接口类型的值都是两个指针的元组:一个指向封闭值的实际类型的指针,以及值本身。在目标平台上,如果其大小≤
uintptr
类型的大小,gc
(和gccgo
,我相信)的实现足够聪明,可以直接将值存储在该指针空间中。 -
(据说更高级):在单独的数据结构(数组或切片)中存储列的类型信息;可以使用
reflect
包中的工具获取类型信息。然后,在行中使用unsafe.Pointer
的数组存储实际数据,并使用与gc
中接口值实现相同的技巧,使用类型转换从/到unsafe.Pointer
。这样可以通过不将每个包含值的值的类型与其一起存储来更节省空间。不确定是否有真正的性能优势。
英文:
Not really an answer but the continuation of the comments thread following the original question…
To implement an in-memory dataset, I would took one of these approaches:
-
(Simple-minded): just use arrays of
interface{}
to store rows.Each value of interface type is a tuple of two pointers: a pointer to the enclosed value's real type and the value itself. Implementation is
gc
(andgccgo
, I beleive) is smart enough to store values directly in that pointer space if its size is ≤ the size ofuintptr
type on the target platform. -
(Supposedly more advanced): store type information for your columns in a separate data structure (an array or slice); the type information might be obtained with tools from the
reflect
package. Then store actual data in rows which are arrays ofunsafe.Pointer
and play the same trick the implementation of interfaces ingc
does with values using type conversions from/tounsafe.Pointer
.This would allow you to be more space-efficient by not storing a type of a value along with each contained value. Not sure about real performance benefits, if any.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论