如何使切片能够将*uint64转换为*int64

huangapple go评论80阅读模式
英文:

How to make a slice to be able to convert *uint64 to *int64

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是一个使用golang的初学者,我对创建子模型的切片仍然感到困惑,之后我想将索引"0"从uint64转换为int64。也许我在转换字段的方式上有误,但我认为在转换之前需要将子模型转换为切片。以下是代码:

params := category.PostAPICategoryParams{
    Data: category.PostAPICategoryBody{
        CategoryData: categoryTestData.CategoryData,
        Childs:       categoryTestData.Childs,
    },
}

result, _ := mainSuite.h.CreateCategory(mainSuite.rt, &params.Data.CategoryData, params.Data.Childs)

categoriesParamsTest := &category.GetAPICategoryParams{
    ParentID: // 这里是我需要编写的代码,
}

在创建切片之前尝试编写代码时出现了错误。

英文:

I'm a beginner in using golang, I'm still confused about creating a slice for the child model, after that I want to take index "0" and convert it from *uint64 to *int64. Maybe i'm wrong way to convert the field, but i think i need to make the child model into a slice first before converting it. Here is the code :

params := category.PostAPICategoryParams{
	Data: category.PostAPICategoryBody{
		CategoryData: categoryTestData.CategoryData,
		Childs:       categoryTestData.Childs,
	},
}

result, _ := mainSuite.h.CreateCategory(mainSuite.rt, &params.Data.CategoryData, params.Data.Childs)

categoriesParamsTest := &category.GetAPICategoryParams{
	ParentID: // here is the code I need to write,
}

Here is the error when I try to write the code before creating the slice :
如何使切片能够将*uint64转换为*int64

答案1

得分: 1

你有一个指向 uint64 的指针,想要一个指向 int64 的指针。没有直接(安全)的方法可以做到这一点,因为指向无符号整数的指针与指向有符号整数的指针不兼容,但是你可以将指针指向的值转换为正确的类型(假设指针不为nil)。

例如:

func convertUnsignedToSignedPointer64(p *uint64) *int64 {
    if p == nil { return nil }
    x := int64(*p)
    return &x
}

你也可以通过 unsafe 包来实现,尽管我在编写代码时尽量避免使用 unsafe 包:

func convertUnsignedToSignedPointer64(p *uint64) *int64 {
    return (*int64)(unsafe.Pointer(p))
}

根据 unsafe.Pointer 中的规则,这是可以的:

(1) 将 *T1 转换为指向 *T2 的指针。

假设 T2 不比 T1 大,并且两者具有等效的内存布局,这种转换允许将一种类型的数据重新解释为另一种类型的数据。

英文:

You have a pointer to a uint64, and want a pointer to a int64. There's no direct (safe) way to do this -- pointers to unsigned ints aren't compatible with pointers to signed ints -- but you can convert the pointed-at value to the right type (assuming the pointer isn't nil).

For example:

func convertUnsignedToSignedPointer64(p *uint64) *int64 {
    if p == nil { return nil }
    x := int64(*p)
    return &x
}

You can also do it via the unsafe package, although I prefer avoiding the unsafe package in code I write when possible:

func convertUnsignedToSignedPointer64(p *uint64) *int64 {
    return (*int64)(unsafe.Pointer(p))
}

This is ok via the rules given in unsafe.Pointer:

> (1) Conversion of a *T1 to Pointer to *T2.
>
> Provided that T2 is no larger than T1 and that the two share an
> equivalent memory layout, this conversion allows reinterpreting data
> of one type as data of another type.

huangapple
  • 本文由 发表于 2022年10月19日 11:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/74119594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定