Go 语言程序设计——面向对象编程(1)

2019-07-18  本文已影响0人  hlemon

几个关键概念

type ColoredPoint struct {
  color.Color // 匿名字段(嵌入)
  x, y    int // 具名字段(聚合)
}

自定义类型

type typeName typeSpecification
type Count int
type StringMap map[string] string
type FloatChan chan float64

var i Count = 7
i++
fmt.Println(i)
sm := make(StringMap)
sm["key1"] = "value1"
sm["key2"] = "value2"
fmt.Println(sm)
fc := make(FloatChan, 1)
fc <- 2.29558714939
fmt.Println(<-fc)
type RuneForRuneFunc func(rune) rune
var removePunctuation RuneForRuneFunc
phrases := []string{"Day; dusk, and night.", "All day long"}
removePunctuation = func(char rune) rune {
  if unicode.Is(unicode.Terminal_punctuation, char) {
    return -1
  }
  return char
}
processPhrases(phrases, removePunctuation)
func processPhrases(phrases []string, function removePunctuation) {
  for _, phrase := range phrases {
    fmt.Println(strings.Map(function, phrase))
  }
}
上一篇 下一篇

猜你喜欢

热点阅读