出现了类型不匹配的错误,uint64和int32类型不匹配。

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

go mismatched types uint64 and int32

问题

我在这里看不出我做错了什么,这个错误是因为两者都是 syscall.Timeval Usec 类型。

谢谢

package common

import (
	"syscall"
)

func getUsecSince(oldTime syscall.Timeval) (result uint64) {
	now := syscall.Timeval{}
	syscall.Gettimeofday(&now)

	for now.Sec > oldTime.Sec {
		result += 1000000
		now.Sec--
	}
	return result + (now.Usec - oldTime.Usec)
}

> ./common.go:15: invalid operation: result + (now.Usec - oldTime.Usec) (mismatched types uint64 and int32)
英文:

I don't see what I'm doing wrong here with this error, both are of type syscall.Timeval Usec

Thanks

package common

import (
	"syscall"
)

func getUsecSince(oldTime syscall.Timeval) (result uint64) {
	now := syscall.Timeval{}
	syscall.Gettimeofday(&now)

	for now.Sec > oldTime.Sec {
		result += 1000000
		now.Sec--
	}
	return result + (now.Usec - oldTime.Usec)
}

> ./common.go:15: invalid operation: result + (now.Usec - oldTime.Usec) (mismatched types uint64 and int32)

答案1

得分: 1

使用有符号的返回值(int64),例如 Timeval.SecTimeval.Usec。为了在不同操作系统之间具有可移植性,请使用 TimevalToNsec。例如,Timeval 字段可能是 int32int64。为了得到正确的结果,请使用以下代码:

package main

import (
	"fmt"
	"syscall"
	"time"
)

func getUsecSince(old syscall.Timeval) int64 {
	var now syscall.Timeval
	syscall.Gettimeofday(&now)
	nsecs := syscall.TimevalToNsec(now) - syscall.TimevalToNsec(old)
	return nsecs / int64(time.Microsecond)
}

func main() {
	old := syscall.Timeval{}
	syscall.Gettimeofday(&old)
	time.Sleep(2*time.Second + 42*time.Microsecond)
	fmt.Println(getUsecSince(old))
}

输出结果为:

2000377
英文:

Use a signed return value (int64), like Timeval.Sec and Timeval.Usec. Use TimevalToNsec for portability across operating systems. For example, Timeval fields may be int32 or int64. For a correct result, use,

package main

import (
	"fmt"
	"syscall"
	"time"
)

func getUsecSince(old syscall.Timeval) int64 {
	var now syscall.Timeval
	syscall.Gettimeofday(&now)
	nsecs := syscall.TimevalToNsec(now) - syscall.TimevalToNsec(old)
	return nsecs / int64(time.Microsecond)
}

func main() {
	old := syscall.Timeval{}
	syscall.Gettimeofday(&old)
	time.Sleep(2*time.Second + 42*time.Microsecond)
	fmt.Println(getUsecSince(old))
}

Output:

2000377

答案2

得分: 1

这个问题的最简单解决方案是:

func getUsecSince(oldTime syscall.Timeval) (result uint64) {
    now := syscall.Timeval{}
    syscall.Gettimeofday(&now)

    // 将时间转换为最小单位,只要在转换过程中没有溢出,就可以轻松忽略边界条件
    result = uint64((now.Sec - oldTime.Sec) * 1000000 + int64(now.Usec - oldTime.Usec))
    return result
}

通过将时间转换为最小单位,只要在转换过程中没有溢出,你就可以轻松忽略边界条件。

请注意,如果你故意使用一个未来的 oldTime 进行测试,这个函数将会失败。如果你想处理这种情况,你需要进行一个检查(将两个时间都转换为 Usec)。

英文:

The simplest solution to this is:

func getUsecSince(oldTime syscall.Timeval) (result uint64) {
    now := syscall.Timeval{}
    syscall.Gettimeofday(&now)

    // Automatically ignore cases like 12.5 - 11.8
    result = uint64((now.Sec - oldTime.Sec) * 1000000 + int64(now.Usec - oldTime.Usec))
    return result
}

By converting to the smallest unit you can ignore the boundary conditions easily as long as there is no overflow during conversion.

Note that if you purposely test the above by using an oldTime in the future, the function will fail. You will need to do a check (with both times converted to Usec) if you want to cover such cases.

答案3

得分: 0

Timeval.Usec被定义为int32。也许result应该也是int32类型?或者,如果你想使用uint64类型,你可以通过uint64(now.Usec - oldTime.Usec)进行类型转换。

英文:

Timeval.Usec is defined as int32. Maybe result should be also int32? Alternatively if you want to use uint64 you can cast it by uint64(now.Usec - oldTime.Usec).

答案4

得分: 0

结果是uint64类型。
其他操作数是int32类型。
运行以下代码:

now := syscall.Timeval{}
syscall.Gettimeofday(&now)
typeUsec := reflect.TypeOf(now.Usec)
fmt.Printf("type %s, value %d \n", typeUsec, now.Usec)

将打印出:

type int32, value 238376

奇怪的是,Go文档中有以下内容:

type Timeval struct {
    Sec  int64
    Usec int64

}

英文:

result is uint64.
The other operands are int32
Running this

now := syscall.Timeval{}
syscall.Gettimeofday(&now)
typeUsec := reflect.TypeOf(now.Usec)
fmt.Printf("type %s, value %d \n", typeUsec, now.Usec)

will print

type int32, value 238376 

Strangely, the go documents have the following

type Timeval struct {
    Sec  int64
    Usec int64

}

huangapple
  • 本文由 发表于 2016年2月26日 07:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/35640525.html
匿名

发表评论

匿名网友

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

确定