在go中定义新类型的目的是什么:
type NewType OldType由于NewType只有方法声明,所以:
var x NewType也可以存储OldType 'objects‘。有什么优势吗?
发布于 2013-07-04 02:01:07
Go编程语言规范
Types
类型确定该类型的值的集合和特定于该类型的值的操作。
您希望标识一组特定的值和操作。
例如,
package main
import "fmt"
type Coordinate float64
type Point struct {
x, y Coordinate
}
func (p *Point) Move(dx, dy Coordinate) {
p.x += dx
p.y += dy
}
func main() {
var p = Point{3.14159, 2.718}
fmt.Println(p)
p.Move(-1, +1)
fmt.Println(p)
}输出:
{3.14159 2.718}
{2.14159 3.718}https://stackoverflow.com/questions/17454993
复制相似问题