Golang中接口内结构体指针的语法错误?怎么回事?
Golang中接口内结构体指针的语法错误?怎么回事? 有人知道为什么在这个文件中会出现语法错误吗?我在其他文件中为Phone、Email、Person等做过同样的操作都没问题。但不知为何这个文件无法接受指向结构体的指针。
package interfaces
//Go library for decoding generic map values into native Go structures.
//github.com/mitchellh/mapstructure
//Utilities for Go structs
//github.com/fatih/structs
//TODO: Create new Public repo and Combine required source from each of these two
//repos. We need to same for Struct and interface and Convert to/from each as well
//as the methods for getting / working with the field, values list, etc.
/*
func (s *struct) ToMap() Map{interface}
func (Map{interface}) FromMap() *struct
func (s *struct) FillInterface(m Map{Interface})
func (Map{interface}) FillStruct(s *struct)
..Incomplete ...
*/
/******************************************************************************************
* Base Interface Definition *
******************************************************************************************/
//BaseStruct is only used for building/testing the base interface and will be removed.
type BaseStruct struct {
testInt int
testBool bool
testString string
testByte []byte
testFloat float64
testComplex complex128
}
//BaseInterface contains the support methods required for interacting with a structure
type BaseInterface interface {
//IsStructValid returns true|false if the current struct data is in valid and complete format
IsStructValid() bool
//IsStringValid returns true|false if the passed in string value is in valid and complete format
IsStringValid(s string) bool
//Get returns the current {model}Struct for the interface
Get()
//Set is given all inteface properties and they are assigned to the {model}Struct
Set()
//Parse takes a string and parses it to find and assign the individual struct properties of
//the interfaces parent
Parse(s string)
//Format returns a formatted string for the interface
Format()
//ToMap Converts {model}Struct to a map[string]interface{}
ToMap()
//FromMap converts map[string]interface{} to {model}Struct
FromMap()
//ToJSON converts (model}Struct to a JSON and returns it
ToJSON()
//FromJSON takes a JSON and converts it to {model}Struct
FromJSON()
}
//TestBaseInterface is only used for building/testing the base interface and will be removed.
type TestBaseInterface interface {
*BaseStruct //<-- Syntax Error: unexpected *, expecting method or interface name? WTF?
BaseInterface
}
//IsStructValid returns true|false if the current struct data is in valid and complete format
func (p *BaseStruct) IsStructValid() bool {}
//IsStringValid returns true|false if the passed in string value is in valid and complete format
func (p *BaseStruct) IsStringValid(s string) {}
//Get returns the current {model}Struct for the interface
func (p *BaseStruct) Get() {}
//Set is given all inteface properties and they are assigned to the {model}Struct
func (p *BaseStruct) Set() {}
//Parse takes a string and parses it to find and assign the individual struct properties of
//the interfaces parent
func (p *BaseStruct) Parse(s string) {}
//Format returns a formatted string for the interface
func (p *BaseStruct) Format() {}
//ToMap Converts {model}Struct to a map[string]interface{}
func (p *BaseStruct) ToMap() {}
//FromMap converts map[string]interface{} to {model}Struct
func (p *BaseStruct) FromMap() {}
//ToJSON converts (model}Struct to a JSON and returns it
func (p *BaseStruct) ToJSON() {}
//FromJSON takes a JSON and converts it to {model}Struct
func (p *BaseStruct) FromJSON() {}
我到底遗漏了什么?
更多关于Golang中接口内结构体指针的语法错误?怎么回事?的实战教程也可以访问 https://www.itying.com/category-94-b0.html
接口由函数和其他接口组成,而不是由指向它们的指针组成。因此,在接口定义中不允许使用指针。
更多关于Golang中接口内结构体指针的语法错误?怎么回事?的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
好的,我明白了。
func main() {
fmt.Println("hello world")
}
没关系,我明白这个意思了,谢谢。 实际上邮件那个例子也是错的。它用了接口(Interface)而其他例子用的是结构体(struct)。我不知道为什么没有显示语法错误,但基本上 BaseTestInterface 需要是一个结构体,该结构体实现了指向该结构体的指针以及任何所需的接口。我想我现在明白了。
NlaakStudios: 那么在Golang中,我应该如何为"属性"定义结构体以及处理这些属性的标准接口?
不应该建立这种耦合关系…
type Aging interface {
SetAge(int)
GetAge() int
}
然后实际的实现者需要决定如何存储必要的状态。
type foo int
func (f *foo) SetAge(age int) {
f = age
}
func (f *foo) GetAge() int {
return int(*f)
}
另外,你的第二个示例在playground上出现了与你原始帖子中示例相同的语法错误…
编辑
PS:我上面的示例代码未经测试,属于半伪代码,但它应该能展示这个思路。
在Go语言中,接口定义不能直接包含结构体指针类型。接口只能包含方法集合,不能包含具体类型(包括结构体指针)。这就是为什么在TestBaseInterface接口中写*BaseStruct会导致语法错误。
正确的做法是让结构体指针实现接口的所有方法。由于BaseStruct已经实现了BaseInterface中的所有方法,*BaseStruct自然就满足了BaseInterface接口。
以下是修正后的代码:
package interfaces
// 其他代码保持不变...
// TestBaseInterface 现在只需要继承 BaseInterface
type TestBaseInterface interface {
BaseInterface
}
// BaseStruct 的方法实现保持不变...
func (p *BaseStruct) IsStructValid() bool {
// 实现代码
return true
}
func (p *BaseStruct) IsStringValid(s string) bool {
// 实现代码
return true
}
// 其他方法实现...
如果你想要一个同时包含特定类型约束的接口,在Go 1.18+中可以使用泛型接口:
// Go 1.18+ 泛型方案
type TestBaseInterface[T any] interface {
BaseInterface
// 可以添加其他约束
}
// 使用示例
func ProcessStruct[T BaseStruct](t T) TestBaseInterface[T] {
// 处理逻辑
return &t
}
或者使用类型断言来确保接口值的具体类型:
func ValidateBaseInterface(bi BaseInterface) (*BaseStruct, bool) {
bs, ok := bi.(*BaseStruct)
return bs, ok
}
在Go中,接口的实现是隐式的 - 只要类型实现了接口中定义的所有方法,它就自动满足该接口。不需要在接口定义中显式声明具体的实现类型。

