在结构字段中计算切片长度。

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

count slice length in struct field

问题

我认为我需要一个比我的case switch更好的解决方案,因为当结构体增加更多字段时,我的函数会变得冗长。有没有一种方法可以用循环替换我的switch?

我有以下代码:

type Things struct {
	StreetNames    []string `json:"streetNames"`
	Letters        []string `json:"letters"`
	MaleNames      []string `json:"maleNames"`
}

func CheckCategories(data *Things, filePath string) error {
	errMsg := "list has no values or is a missing category in File: " + filePath
	categories := []string{"street_name", "letter", "male_first_name"}
	for _, value := range categories {
		switch value {
		case "street_name":
			if len(data.StreetNames) == 0 {
				return errors.New("street_name " + errMsg)
			}
		case "letter":
			if len(data.Letters) == 0 {
				return errors.New("letter " + errMsg)
			}
		case "male_first_name":
			if len(data.MaleNames) == 0 {
				return errors.New("male_first_name " + errMsg)
			}
		}
	}
	return nil
}

这对我来说是有效的,但是真正的结构体包含12个字段,这使得我的函数变得冗长和重复。

我尝试过:

for _, value := range categories {
	if len("data." + value) == 0 {
		return errors.New(value + errMsg)
	}
}

但当我运行代码时,我花了一些时间才注意到它没有按预期工作,我得到的是字符串的长度。我尝试过data[value],但也没有起作用。

英文:

I think I need a better solution than my case switch as the struct gains more fields my function will become verbose. Is there a way to swap my switch for a loop?

I have the following code

type Things struct {
	StreetNames    []string `json:"streetNames"`
	Letters        []string `json:"letters"`
	MaleNames      []string `json:"maleNames"`
}

func CheckCategories(data *Things, filePath string) error {
	errMsg := "list has no values or is a missing category in File: " + filePath
	categories := []string{"street_name", "letter", "male_first_name"}
	for _, value := range categories {
		switch value {
		case "street_name":
			if len(data.StreetNames) == 0 {
				return errors.New("street_name " + errMsg)
			}
		case "letter":
			if len(data.Letters) == 0 {
				return errors.New("letter " + errMsg)
			}
		case "male_first_name":
			if len(data.MaleNames) == 0 {
				return errors.New("male_first_name " + errMsg)
			}
		}
	}
	return nil
}

This works for me but the real struct contains 12 fields which makes my functions long and repetitive.

I tried

for _, value := range categories {
		if len("data." + value) == 0 {
			return errors.New(value + errMsg)
}

But when I ran the code I took a moment to notice it wasn't working as intended, Im getting the length of the string. I have tried data[value] but that didn't work either.

答案1

得分: 1

有一种方法可以将我的开关换成一个循环吗?

你可以这样做:

type Things struct {
    StreetNames []string `json:"streetNames"`
    Letters     []string `json:"letters"`
    MaleNames   []string `json:"maleNames"`
}

func CheckCategories(data *Things, filePath string) error {
    errMsg := "list has no values or is a missing category in File: " + filePath
    categories := []struct{
        name  string
        slice []string
    }{
        {"street_name", data.StreetNames},
        {"letter", data.Letters},
        {"male_first_name", data.MaleNames},
    }

    for _, v := range categories {
        if len(v.slice) == 0 {
            return errors.New(v.name + " " + errMsg)
        }
    }
    return nil
}
英文:

> Is there a way to swap my switch for a loop?

You could do the following:

type Things struct {
    StreetNames []string `json:"streetNames"`
    Letters     []string `json:"letters"`
    MaleNames   []string `json:"maleNames"`
}

func CheckCategories(data *Things, filePath string) error {
    errMsg := "list has no values or is a missing category in File: " + filePath
    categories := []struct{
        name  string
        slice []string
    }{
        {"street_name", data.StreetNames},
        {"letter", data.Letters},
        {"male_first_name", data.MaleNames},
    }

    for _, v := range categories {
        if len(v.slice) == 0 {
            return errors.New(v.name + " " + errMsg)
        }
    }
    return nil
}

huangapple
  • 本文由 发表于 2022年11月30日 23:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/74630179.html
匿名

发表评论

匿名网友

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

确定