Go – 验证的数据类型

huangapple go评论117阅读模式
英文:

Go - Data types for validation

问题

如何为Go创建一个新的数据类型,在创建新变量(该类型的变量)时可以检查/验证其模式?

例如,要验证字符串是否有20个字符,我尝试了以下代码:

  1. // 格式:2006-01-12T06:06:06Z
  2. func date(str string) {
  3. if len(str) != 20 {
  4. fmt.Println("错误")
  5. }
  6. }
  7. var Date = date()
  8. type Account struct {
  9. domain string
  10. username string
  11. created Date
  12. }

但是它失败了,因为Date不是一个类型。

英文:

How to create a new data type for Go which to can check/validate its schema when is created a new variable (of that type)?

By example, to validate if a string has 20 characters, I tried:

  1. // Format: 2006-01-12T06:06:06Z
  2. func date(str string) {
  3. if len(str) != 20 {
  4. fmt.Println("error")
  5. }
  6. }
  7. var Date = date()
  8. type Account struct {
  9. domain string
  10. username string
  11. created Date
  12. }

but it fails because Date is not a type.

答案1

得分: 3

在你的示例中,你将Date定义为一个变量,然后尝试将其用作类型。

我猜你想做的是这样的。

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. type Date int64
  8. type Account struct {
  9. domain string
  10. username string
  11. created Date
  12. }
  13. func NewDate(date string) (Date, os.Error) {
  14. // 日期格式:2006-01-12T06:06:06Z
  15. if len(date) == 0 {
  16. // 默认为今天
  17. today := time.UTC()
  18. date = today.Format(time.ISO8601)
  19. }
  20. t, err := time.Parse(time.ISO8601, date)
  21. if err != nil {
  22. return 0, err
  23. }
  24. return Date(t.Seconds()), err
  25. }
  26. func (date Date) String() string {
  27. t := time.SecondsToUTC(int64(date))
  28. return t.Format(time.ISO8601)
  29. }
  30. func main() {
  31. var account Account
  32. date := "2006-01-12T06:06:06Z"
  33. created, err := NewDate(date)
  34. if err == nil {
  35. account.created = created
  36. } else {
  37. fmt.Println(err.String())
  38. }
  39. fmt.Println(account.created)
  40. }
英文:

In your example, you defined Date as a variable and then tried to use it as a type.

My guess is that you want to do something like this.

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. type Date int64
  8. type Account struct {
  9. domain string
  10. username string
  11. created Date
  12. }
  13. func NewDate(date string) (Date, os.Error) {
  14. // date format: 2006-01-12T06:06:06Z
  15. if len(date) == 0 {
  16. // default to today
  17. today := time.UTC()
  18. date = today.Format(time.ISO8601)
  19. }
  20. t, err := time.Parse(time.ISO8601, date)
  21. if err != nil {
  22. return 0, err
  23. }
  24. return Date(t.Seconds()), err
  25. }
  26. func (date Date) String() string {
  27. t := time.SecondsToUTC(int64(date))
  28. return t.Format(time.ISO8601)
  29. }
  30. func main() {
  31. var account Account
  32. date := "2006-01-12T06:06:06Z"
  33. created, err := NewDate(date)
  34. if err == nil {
  35. account.created = created
  36. } else {
  37. fmt.Println(err.String())
  38. }
  39. fmt.Println(account.created)
  40. }

答案2

得分: 1

您可能需要使用标准库中的Time类型。文档

英文:

You probably want the Time type from the standard library. Documentation.

huangapple
  • 本文由 发表于 2010年4月14日 18:12:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/2636485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定