Type Declaration

Overview

The API type declaration is very similar to the Golang type statement, but there are some nuances and we turn to the API type statement.

Type Declaration

In the API description, the type declaration needs to satisfy the following rule:

  1. Type declaration must start with type
  2. No need to declare structurekeywords
  3. Nested Structural Declarations are not supported
  4. Alias not supported

Sample

  1. type StructureExample {
  2. // Basic data type example
  3. BaseInt int `json:"base_int"`
  4. BaseBool bool `json:"base_bool"`
  5. BaseString string `json:"base_string"`
  6. BaseByte byte `json:"base_byte"`
  7. BaseFloat32 float32 `json:"base_float32"`
  8. BaseFloat64 float64 `json:"base_float64"`
  9. // slice example
  10. BaseIntSlice []int `json:"base_int_slice"`
  11. BaseBoolSlice []bool `json:"base_bool_slice"`
  12. BaseStringSlice []string `json:"base_string_slice"`
  13. BaseByteSlice []byte `json:"base_byte_slice"`
  14. BaseFloat32Slice []float32 `json:"base_float32_slice"`
  15. BaseFloat64Slice []float64 `json:"base_float64_slice"`
  16. // map example
  17. BaseMapIntString map[int]string `json:"base_map_int_string"`
  18. BaseMapStringInt map[string]int `json:"base_map_string_int"`
  19. BaseMapStringStruct map[string]*StructureExample `json:"base_map_string_struct"`
  20. BaseMapStringIntArray map[string][]int `json:"base_map_string_int_array"`
  21. // anonymous example
  22. *Base
  23. // pointer example
  24. Base4 *Base `json:"base4"`
  25. // new features ( goctl >= 1.5.1 version support )
  26. // tag ignore example
  27. TagOmit string
  28. }
Type Declaration - 图1tip

New API features are referenced New API Resolver Us

We don’t support generic, weak types, e.g. any type

You can participate in the discussion discussion to share your views https://github.com/zeroicro/go-zero/discussions/3121 ::