You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package goSyntax
// 基本接口,方法集
type ReadWriter interface {
Read(p []byte) (int, error)
Write(p []byte) (int, error)
}
// 一般接口,仅包含类型集(类型约束)
type Float interface {
~float32 | ~float64
}
// 一般接口,同时包含方法集合类型集的接口
type FloatReadWriter interface {
~float32 | ~float64
Read(p []byte) (int, error)
Write(p []byte) (int, error)
}
// 实现接口
// 基本接口,方法集
type rw struct{}
func (rw) Read(p []byte) (int, error) { return 0, nil }
func (rw) Write(p []byte) (int, error) { return 0, nil }
// 实现接口
// 一般接口,类型集
type myFloat float32
func (myFloat) Read(p []byte) (int, error) { return 0, nil }
func (myFloat) Write(p []byte) (int, error) { return 0, nil }
type yourFloat float64
// 调用测试
func funcMS(p ReadWriter) {
}
// interface includes constraint elements '~float32', '~float64', can only be used in type parameters
// func funcGI(p Float) {}
func funcGI[T Float]() {}
func funcGI2[T FloatReadWriter]() {}
// 测试函数
func TypeSet() {
// 传统的接口(基础接口)
funcMS(rw{})
// myFloat, yourFloat, float32, float644中类型的基础类型满足Float接口
funcGI[myFloat]()
funcGI[yourFloat]()
funcGI[float32]()
funcGI[float64]()
// myFloat 满足以float32为基础类型同时实现了Read和Write
funcGI2[myFloat]()
// 而 yourFloat, float32, float64没有实现Read和Write
//funcGI2[yourFloat]()
//funcGI2[float32]()
//funcGI2[float64]()
}
type Empty interface {
int
string
}
type myMap[K comparable, V any] map[K]V
//type yourMap[K any, V any] map[K]V