Golang中的for each循环筛选并存入新变量中。

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

Golang for each filtering into a new var

问题

我正在使用for each循环和信息的var,并通过A)正则表达式匹配字符串B)时间比较进行过滤。过滤工作得很好,我得到了所需的数据,但目前我是通过循环的一部分使用fmt.Println将其输出到屏幕上。我的目标是将这些数据取出并构建另一个带有已过滤列表的var。我猜我需要创建一个新的变量并将其添加到其中?但是如何返回它并在以后使用它呢?

任何帮助都将不胜感激。

   for _, thing := range things {
 
       if thing.element1 != nil {
 
           matched, err := regexp.MatchString(z, element1)
 
           if err != nil {
               fmt.Println(err)
           }
 
           if matched {
 
               if timecomparrison(element2, a) {
 
                  
                   // 这是需要添加新变量并作为变量返回的部分
                   fmt.Println("****")
                   fmt.Println("element1:", element1)
                   fmt.Println("element2:", element2)
 
               }
           }
       }
   }
 
}

英文:

Im working with for each loop and var of information and filtering it by A) regex.matchString B)Timecomparrison. The filtering works well and I have the data I need but currently I'm outputting it to screen via fmt.Println in part of the loop. My goal is to take that data and build another var with the now filtered list. I guess I need to make a new variable and add to it? But how do I return that and something I can use later?

Any assistance is appreciated.

   for _, thing := range things {
 
       if thing.element1 != nil {
 
           matched, err := regexp.MatchString(z, element1)
 
           if err != nil {
               fmt.Println(err)
           }
 
           if matched {
 
               if timecomparrison(element2, a) {
 
                  
                   // this is a section that needs to be added new var and returned as a var
                   fmt.Println("****")
                   fmt.Println("element1:", element1)
                   fmt.Println("element2:", element2)
 
               }
           }
       }
   }
 
}

答案1

得分: 2

我认为你需要这样的代码。

type Thing struct {
    element1 string
    element2 string
}

func filter() []Thing {
    things := []Thing{
        {element1: "element1", element2: "element2"},
    }
    var result []Thing
    regex := "/{}d/"
    date := time.Now()
    for _, thing := range things {
        if thing.element1 != "" {
            matched, err := regexp.MatchString(regex, thing.element1)
            if err != nil {
                fmt.Println(err)
            }
            if matched {
                if timeComparison(thing.element2, date) {
                    // 这是一个需要添加新变量并作为变量返回的部分
                    fmt.Println("****")
                    fmt.Println("element1:", thing.element1)
                    fmt.Println("element2:", thing.element2)
                    result = append(result, thing)
                }
            }
        }
    }
    return result
}

我清理了代码,添加了一个类型和一些数据,修复了一些问题并重命名了一些内容,但你应该能理解这个代码的意思 Golang中的for each循环筛选并存入新变量中。

英文:

I think you need something like this.

type Thing struct {
	element1 string
	element2 string
}

func filter() []Thing {
	things := []Thing{
		{element1: "element1", element2: "element2"},
	}
	var result []Thing
	regex := "/{}d/"
	date := time.Now
	for _, thing := range things {
		if thing.element1 != nil {
			matched, err := regexp.MatchString(regex, thing.element1)
			if err != nil {
				fmt.Println(err)
			}
			if matched {
				if timeComparison(thing.element2, date) {
					// this is a section that needs to be added new var and returned as a var
					fmt.Println("****")
					fmt.Println("element1:", thing.element1)
					fmt.Println("element2:", thing.element2)
					result = append(result, thing)
				}
			}
		}
	}
	return result
}

I cleaned the code, added a type and some data, fixed some issues and renamed some things, but you should get the idea Golang中的for each循环筛选并存入新变量中。

huangapple
  • 本文由 发表于 2021年12月10日 07:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/70297943.html
匿名

发表评论

匿名网友

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

确定