1. package main
  2. import "fmt"
  3. type Traverser func(ele interface{})
  4. type UserId string // <-- new type
  5. type ProductId string
  6. func AddProduct(UserId UserId, productId ProductId) {
  7. fmt.Println(UserId, productId)
  8. }
  9. func main() {
  10. userId := UserId("some-user-id")
  11. productId := ProductId("some-product-id")
  12. // Right order: all fine
  13. AddProduct(userId, productId)
  14. // Wrong order: would compile with raw strings
  15. AddProduct(productId, userId)
  16. // Compilation errors:
  17. // cannot use productId (type ProductId) as type UserId in argument to AddProduct
  18. // cannot use userId (type UserId) as type ProductId in argument to AddProduct
  19. }

分类: web

标签:   golang