如何将 *int64 转换为 *int32?

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

how to convert *int64 to *int32?

问题

我在函数中有一个参数是 *int64,我需要将其转换为 *int32,以满足我想要调用的函数的定义。

func t1(x *int64) error {
    var y *int32   
    t2(y)
}
func t2(y *int32) error {}

另外,如果我想将 *int32 转换为 *int8,是否需要另一个函数?有没有办法处理这样的问题?

英文:

I have a *int64 as one of the parameters in my function, and I need to convert it to *int32 to meet the definition of the function I want to call.

func t1(x *int64) error {
    var y *int32   
    t2(y)
}
func t2(y *int32) error {}

And, if I want to convert *int32 to *int8, then do I need another function? Is there a way to handle such problems?

答案1

得分: 2

如何将int64转换为int32?

你不能直接将int64转换为int32。

可以使用以下代码进行转换:

var i32 int32 = int32(*x)
t2(&i32)
*x = int64(i32)

请注意,这只是一种可能的方法,具体取决于你的需求和上下文。

英文:

> [H]ow to convert *int64 to *int32?

You cannot.

Use var i32 int32 = int32(*x); t2(&i32); *x=int64(i32).

huangapple
  • 本文由 发表于 2022年2月23日 21:12:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/71237598.html
匿名

发表评论

匿名网友

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

确定