英文:
Conditional variable declaration in golang?
问题
在Golang中,不支持像你提到的那样进行条件变量类型声明。但是,你可以通过使用接口来实现类似的效果。你可以定义一个接口,然后让NormalResult和AdminResult都实现该接口。然后,在doSomething函数中,将参数类型声明为该接口类型。这样,你就可以根据条件来传递不同的结构体实例给doSomething函数了。
以下是修改后的代码示例:
type Result interface {
// 定义接口方法
}
type NormalResult struct {
// NormalResult 结构体的字段
}
func (nr NormalResult) Method1() {
// 实现接口方法
}
type AdminResult struct {
// AdminResult 结构体的字段
}
func (ar AdminResult) Method1() {
// 实现接口方法
}
func main() {
var result Result
if isAdmin {
result = NormalResult{}
} else {
result = AdminResult{}
}
// do something to &result
doSomething(&result)
}
func doSomething(r Result) {
// something
r.Method1()
}
希望对你有帮助!
英文:
Is it possible to do conditional variable type declaration like this in Golang?
if isAdmin {
var result NormalResult
} else {
var result AdminResult
}
// do something to &result
doSomething(&result)
func doSomething(interface{}) {
// something
}
The above does not work, but the ideas is that normalResult and adminResults are very similar structs and how would I go about doing this?
Thank you!
答案1
得分: 12
不,在这种方式下不行。Go是静态类型的语言,需要在编译时知道类型信息。
你可以将result
声明为某种类型的接口,该接口可以满足AdminResult和NormalResult两种类型。然后你可以在运行时使用类型断言来确定它是哪种类型的结果。
(你还必须在if块之外声明result
,因为Go是块作用域的)
type NormalResult struct {
Value int
}
func (r NormalResult) Result() int {
return r.Value
}
type AdminResult struct {
Value int
}
func (r AdminResult) Result() int {
return r.Value
}
type Resulter interface {
Result() int
}
func main() {
isAdmin := true
var r Resulter
if isAdmin {
r = AdminResult{2}
} else {
r = NormalResult{1}
}
fmt.Println("Hello, playground", r)
}
英文:
No, not in this manner. Go being statically typed, needs to know the type information at compile time.
What you could do is declare result
as an interface of some type which both AdminResult and NormalResult satisfy. You can then use a type assertion at runtime to decide which type of result it is.
(You also have to declare result
outside of the if blocks because Go is block scoped)
type NormalResult struct {
Value int
}
func (r NormalResult) Result() int {
return r.Value
}
type AdminResult struct {
Value int
}
func (r AdminResult) Result() int {
return r.Value
}
type Resulter interface {
Result() int
}
func main() {
isAdmin := true
var r Resulter
if isAdmin {
r = AdminResult{2}
} else {
r = NormalResult{1}
}
fmt.Println("Hello, playground", r)
}
答案2
得分: 7
根据相似性的不同类型,你可能有不同的选择。
使用嵌入结构体
根据你的结构,你可以使用嵌入结构体。假设NormalResult
的定义如下:
type NormalResult struct {
Name string
Value int
}
如果AdminResult
与NormalResult
共享相同的属性,只是添加了一些额外的属性(比如UserId
),你可以选择将NormalResult
嵌入到AdminResult
中,像这样:
type AdminResult struct {
*NormalResult
UserId int64
}
然后,你还可以为NormalResult
声明方法,这些方法也会被提升到AdminResult
中:
func (r *NormalResult) doSomething() {
// 做一些操作
}
编辑
不,正如你所建议的,Go语言中不可能有条件类型。一个变量只能是一个类型,无论是NormalResult
、AdminResult
还是interface{}
。
英文:
Depending on what kind of similarities, you might have different options.
Using embedded structs
Depending on your structure, you might be able to use embedded structs. Let's say NormalResult is defined like this:
type NormalResult struct {
Name string
Value int
}
And if AdminResult shares the same properties but just adds a few more of them (like UserId), you can choose to embed NormalResult into the AdminResult like this:
type AdminResult struct {
*NormalResult
UserId int64
}
Then you can also declare methods for NormalResult
which will be promoted to AdminResult as well:
func (r *NormalResult) doSomething() {
// Doing something
}
Edit
And, no, it is not possible to have conditional types in Go as you suggested. A variable can only be of one type, be it NormalResult
, AdminResult
or interface{}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论