简体   繁体   English

Go中等效的sizeof(aType)

[英]Equivalent of sizeof(aType) in Go

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. C ++和其他几种语言都有一个名为sizeof(int)的函数(或者你需要的任何类型),它返回当前系统中特定数据类型消耗的字节数。

Is there an equivalent function in Go? Go中是否有相同的功能? What is it? 它是什么?

The equivalent of sizeof in go is unsafe.Sizeof . go中sizeof的等价物是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). 它与C中的sizeof之间的一个区别在于它仅在值上定义(而在C中,它适用于值和类型)。 The other main difference is that in go it's hardly ever needed, whereas in C it's fairly common. 另一个主要区别在于,它几乎不需要,而在C中则相当常见。

An example is: 一个例子是:

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    fmt.Println(unsafe.Sizeof(int(0)))
}

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. 但我认为你会发现sizeof函数不像C中那样经常需要。

If you simply want to find out the size of an int or uint , use strconv.IntSize . 如果您只想查找intuint的大小,请使用strconv.IntSize

Package strconv 包strconv

Constants 常量

 const IntSize = intSize 

IntSize is the size in bits of an int or uint value. IntSizeintuint值的位大小。

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

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. (头脑简单):只需使用interface{}数组来存储行。

    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 (and gccgo , I beleive) is smart enough to store values directly in that pointer space if its size is ≤ the size of uintptr type on the target platform. 实现是gc (和gccgo ,我相信)足够智能,如果它的大小≤目标平台上的uintptr类型的大小,则直接在该指针空间中存储值。

  • (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. 可以使用reflect包中的工具获取类型信息。 Then store actual data in rows which are arrays of unsafe.Pointer and play the same trick the implementation of interfaces in gc does with values using type conversions from/to unsafe.Pointer . 然后将实际数据存储在unsafe.Pointer数组的行中,并使用gc中的接口实现与使用/ to unsafe.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. 如果有的话,不确定真正的性能优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM