Golang多重括号的for循环

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

Golang for loop with multiple brackets

问题

这是一个在Go语言中常见的结构,被称为"匿名结构体切片"。在这个例子中,使用了一个匿名结构体切片来定义一个包含两个字段的测试数据集合。每个测试数据都包含一个"missingField"字段和一个"errorMessage"字段。

在for循环中,通过range关键字遍历这个匿名结构体切片。每次迭代,将当前迭代的测试数据赋值给变量test。然后,根据test中的字段值执行相应的操作。

这种结构的好处是可以方便地定义和使用临时的测试数据集合,而不需要为每个测试数据定义一个具名的结构体类型。这样可以简化代码并提高可读性。

英文:

I've stumbled upon this repository,

https://github.com/prometheus-community/jiralert/blob/a0f0e80e575e71cbf7db565d3296a3a984282dff/pkg/config/config_test.go#L148

The for loop has multiple brackets:

for _, test := range []struct {
		missingField string
		errorMessage string
	}{
		{"Name", "missing name for receiver"},
    (...)
	} {

		fields := removeFromStrSlice(mandatory, test.missingField)

	(...)
		}
		configErrorTestRunner(t, config, test.errorMessage)
	}

I haven't been able to find anything about this in the go documentation, what is this construct?

答案1

得分: 2

第一对括号是结构体类型定义的一部分,其语法如下:

struct{
    field1 type1
    field2 type2
    ...
}

第二对括号是复合字面量值的一部分,用于创建该结构体的slice的值。它的语法如下:

[]elementType{value1, value2, ...}

内部嵌套的括号是复合字面量创建结构体切片的值的一部分。类型被省略(从切片类型中已知),所以每个{field1Value, fieldValue2...}都是一个结构体值。

第三对括号定义了for语句的代码块。for语句遍历由上述复合字面量定义的切片的元素。

英文:

The first bracket pair is part of the struct type definition which has the syntax:

struct{
    field1 type1
    field2 type2
    ...
}

The second pair is part of the composite literal value, creating a value for the slice of this struct. It has the syntax of:

[]elementType{value1, value2, ...}

The inner, embedded brackets are part of the composite literals creating the struct values of the slice. The type is omitted (it's known from the slice type), so each {field1Value, fieldValue2...} is a struct value.

The third pair defines the block of the for statement. The for statement iterates over the elements of the slice defined by the mentioned composite literal.

答案2

得分: 1

为了提高代码的可读性和理解性,可以进行以下清理:

type TestStruct struct {
  missingField string
  errorMessage string
}

testCase := []TestStruct{
  {
    missingField: "Name",
    errorMessage: "missing name for receiver",
  },
  // ...
}

for _, test := range testCase {
  fields := removeFromStrSlice(mandatory, test.missingField)
}

configErrorTestRunner(t, config, test.errorMessage)

configErrorTestRunner(t, config, test.errorMessage) 可能是从父级测试函数中调用的。

英文:

To clean up the code for better readability and understanding. Whatever you gave was equivalent to:

type TestStruct struct {
  missingField string
  errorMessage string
}

testCase := TestStruct {
  {
    missingField: "Name",
    errorMessage: "missing name for receiver",
  }
  (...)
}


for _, test := range(testCase) {

  fields := removeFromStrSlice(mandatory, test.missingField)
}

configErrorTestRunner(t, config, test.errorMessage) is probably from a parent test func

huangapple
  • 本文由 发表于 2022年8月21日 18:44:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/73433673.html
匿名

发表评论

匿名网友

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

确定