How can I input data into a mysql model from an array in GO?

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

How can I input data into a mysql model from an array in GO?

问题

所以,我正在使用GO和GORM开发这个MySQL模型,用户可以输入他们的数据。

  1. package table
  2. import (
  3. // "gorm.io/driver/mysql"
  4. "gorm.io/gorm"
  5. )
  6. var Department = []string{"计算机科学", "工程", "医学科学"}
  7. type Account struct {
  8. gorm.Model
  9. Accountname string `json:"name"` //AccountName
  10. AccontNumber int64 `json:"number"` //AccountNumber Text(40)
  11. //需要用户从Department数组中选择一个值
  12. }

我希望用户可以从提供的Department数组中选择一个值,并将其存储在我的表中,我该如何做到这一点?

英文:

So, I'm working on this mysql model Using GO and GORM where the user can input their data

  1. `package table
  2. import (
  3. //"gorm.io/driver/mysql"
  4. "gorm.io/gorm"
  5. )
  6. var Department = []string{"Computer Science", "Engineering", "Medical Science"}
  7. type Account struct {
  8. gorm.Model
  9. Accountname string `json:"name"` //AccountName
  10. AccontNumber int64 ` json:"number"` //AccountNumber Text(40)
  11. //need users to select one of the values from the Department Array
  12. }`

I want my user to select a value from the ones provided in the Department array and store it in my table, how can I go about doing it?

答案1

得分: 2

你可以为部门定义一个自定义字符串类型,但你仍然需要显式地检查用户提供的字符串是否正确:

  1. type DepartmentName string
  2. const (
  3. DepartmentComputerScience DepartmentName = "计算机科学"
  4. DepartmentNameEngineering DepartmentName = "工程"
  5. DepartmentNameMedicalScience DepartmentName = "医学科学"
  6. )
  7. var DepartmentsMap = map[DepartmentName]bool{
  8. DepartmentComputerScience: true,
  9. DepartmentNameEngineering: true,
  10. DepartmentNameMedicalScience: true,
  11. }
  12. type Account struct {
  13. gorm.Model
  14. Accountname string `json:"name"` //AccountName
  15. AccontNumber int64 `json:"number"` //AccountNumber Text(40)
  16. Department DepartmentName `json:"department"`
  17. }
  18. func (d DepartmentName) Valid() error {
  19. if _, ok := DepartmentsMap[d]; ok {
  20. return nil
  21. }
  22. return errors.New("无效的部门")
  23. }
英文:

You can define a custom string type for departments, but you still will have to explicitly check that the user provided string is correct:

  1. type DepartmentName string
  2. const (
  3. DepartmentComputerScience DepartmentName "Computer Science"
  4. DepartmentNameEngineering DepartmentName "Engineering"
  5. DepartmentNameMedicalScience DepartmentName "Medical Science"
  6. )
  7. var DepartmentsMap = map[DepartmentName]bool{
  8. DepartmentComputerScience: true,
  9. DepartmentNameEngineering: true,
  10. DepartmentNameMedicalScience: true,
  11. }
  12. type Account struct {
  13. gorm.Model
  14. Accountname string `json:"name"` //AccountName
  15. AccontNumber int64 ` json:"number"` //AccountNumber Text(40)
  16. Department DepartmentName `json:"department"`
  17. }
  18. func (d DepartmentName) Valid() error {
  19. if _, ok := DepartmentsMap[d]; ok {
  20. return nil
  21. }
  22. return errors.New("invalid department")
  23. }

huangapple
  • 本文由 发表于 2022年5月17日 16:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/72270894.html
匿名

发表评论

匿名网友

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

确定