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.
golang-tutorial/demo/interface/demo01.go

31 lines
526 B

package main
import (
"fmt"
)
// interface define 元音,接口中只能定义方法哦
type VowelsFinder interface {
FindVowels() []rune
}
type MyString string
// 方法
func (ms MyString) FindVowels() []rune {
var vowels []rune
for _, rune := range ms {
if rune == 'a' || rune == 'e' || rune == 'i' || rune == 'o' || rune == 'u' {
vowels = append(vowels, rune)
}
}
return vowels
}
func main() {
name := MyString("Sam Anderson")
var v VowelsFinder
v = name
fmt.Printf("Vowels are %c", v.FindVowels())
}