gpt4 book ai didi

go - 在包之间使用相同结构的类型断言

转载 作者:IT王子 更新时间:2023-10-29 01:56:58 26 4
gpt4 key购买 nike

我很难理解 Go 中的某些类型断言以及为什么下面的代码不起作用并导致 panic 。

panic: interface conversion: interface {} is []db.job, not []main.job

主要内容:

/*stackTypeAssert.go
> panic: interface conversion: interface {} is []db.job, not []main.job
*/

package main

import (
"fmt"
"stackTypeAssert/db"
)

type job struct {
ID int
Status string
}

type jobs interface{}

func main() {
jobTable := db.GetJobs()
fmt.Println(jobTable) // This works: [{1 pending} {2 pending}]

//Type Assertion
var temp []job
//panic: interface conversion: interface {} is []db.job, not []main.job
temp = jobTable.([]job)
fmt.Println(temp)
}

包数据库:

/*Package db ...
panic: interface conversion: interface {} is []db.job, not []main.job
*/
package db

//GetJobs ...
func GetJobs() interface{} {
//Job ...
type job struct {
ID int
Status string
}
task := &job{}
var jobTable []job
for i := 1; i < 3; i++ {
*task = job{i, "pending"}
jobTable = append(jobTable, *task)
}
return jobTable
}

最佳答案

Import declarations 的语言规范中它被描述为:-

The PackageName is used in qualified identifiers to access exported identifiers of the package within the importing source file. It is declared in the file block. If the PackageName is omitted, it defaults to the identifier specified in the package clause of the imported package. If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block and must be accessed without a qualifier.

正如错误所说:-

panic: interface conversion: interface {} is []db.job, not []main.job

您应该通过将 db 包的结构导入 main 来创建临时变量,因为返回值是一个包装结构 db.jobs 的接口(interface),而不是 main.jobs

package main

import (
"fmt"
"stackTypeAssert/db"
)

type job struct {
ID int
Status string
}

type jobs interface{}

func main() {
jobTable := db.GetJobs()
fmt.Println(jobTable) // This works: [{1 pending} {2 pending}]

// create a temp variable of []db.Job type
var temp []db.Job
// get the value of interface returned from `GetJobs` function in db package and then use type assertion to get the underlying slice of `db.Job` struct.
temp = jobTable.(interface{}).([]db.Job)
fmt.Println(temp)
}

db 包文件中定义 GetJobs() 函数之外的结构,并通过将结构转换为 大写 使其可导出。

package db
// make it exportable by converting the name of struct to uppercase
type Job struct {
ID int
Status string
}

//GetJobs ...
func GetJobs() interface{} {
task := &Job{}
var jobTable []Job
for i := 1; i < 3; i++ {
*task = Job{i, "pending"}
jobTable = append(jobTable, *task)
}
return jobTable
}

输出

[{1 pending} {2 pending}]
[{1 pending} {2 pending}]

有关导出标识符的更多信息,您可以查看此链接 Exported functions from another package

关于go - 在包之间使用相同结构的类型断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51206481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com