如何将字段作为参数传递

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

How to pass field as parameter

问题

我想将字段作为参数传递给函数来返回值。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type s struct {
  6. a int
  7. b int
  8. }
  9. func c(s s, field string) int {
  10. var t int
  11. if field == "a" {
  12. t = s.a
  13. } else if field == "b" {
  14. t = s.b
  15. }
  16. return t
  17. }
  18. func main() {
  19. fmt.Println(c(s{5, 8}, "b"))
  20. }

有时我想让t = s.a,有时我想让t = s.b来返回值8,问题是如何将它作为参数传递。

英文:

i want to pass field as parameter to return value from function

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type s struct {
  6. a int
  7. b int
  8. }
  9. func c(s s) int {
  10. var t int
  11. t = s.a // how to change this to t=s.b just by pass parameter
  12. return t
  13. }
  14. func main() {
  15. fmt.Println(c(s{5, 8}))
  16. }

some times i want to make t = s.a and other time i wanted t = s.b to return value 8 the question is how to pass it like parameter

https://play.golang.org/p/JisnrTxF2EY

答案1

得分: 2

你可以添加第二个参数来指示你想要的字段,例如:

  1. func c2(s s, field int) int {
  2. var t int
  3. switch field {
  4. case 0:
  5. t = s.a
  6. case 1:
  7. t = s.b
  8. }
  9. return t
  10. }

或者更方便的方法是传递字段的名称,并使用反射来获取该字段:

  1. func c3(s s, fieldName string) int {
  2. var t int
  3. t = int(reflect.ValueOf(s).FieldByName(fieldName).Int())
  4. return t
  5. }

或者你可以传递字段的地址,并赋值指向的值:

  1. func c4(f *int) int {
  2. var t int
  3. t = *f
  4. return t
  5. }

测试上述解决方案:

  1. x := s{5, 8}
  2. fmt.Println("c2 with a:", c2(x, 0))
  3. fmt.Println("c2 with b:", c2(x, 1))
  4. fmt.Println("c3 with a:", c3(x, "a"))
  5. fmt.Println("c3 with b:", c3(x, "b"))
  6. fmt.Println("c4 with a:", c4(&x.a))
  7. fmt.Println("c4 with b:", c4(&x.b))

输出结果为:

  1. c2 with a: 5
  2. c2 with b: 8
  3. c3 with a: 5
  4. c3 with b: 8
  5. c4 with a: 5
  6. c4 with b: 8

你可以在Go Playground上尝试运行以上代码。

英文:

You may add a 2nd parameter to signal which field you want, for example:

  1. func c2(s s, field int) int {
  2. var t int
  3. switch field {
  4. case 0:
  5. t = s.a
  6. case 1:
  7. t = s.b
  8. }
  9. return t
  10. }

Or a more convenient way is to pass the name of the field, and use reflection to get that field:

  1. func c3(s s, fieldName string) int {
  2. var t int
  3. t = int(reflect.ValueOf(s).FieldByName(fieldName).Int())
  4. return t
  5. }

Or you may pass the address of the field, and assign the pointed value:

  1. func c4(f *int) int {
  2. var t int
  3. t = *f
  4. return t
  5. }

Testing the above solutions:

  1. x := s{5, 8}
  2. fmt.Println("c2 with a:", c2(x, 0))
  3. fmt.Println("c2 with b:", c2(x, 1))
  4. fmt.Println("c3 with a:", c3(x, "a"))
  5. fmt.Println("c3 with b:", c3(x, "b"))
  6. fmt.Println("c4 with a:", c4(&x.a))
  7. fmt.Println("c4 with b:", c4(&x.b))

Which will output (try it on the Go Playground):

  1. c2 with a: 5
  2. c2 with b: 8
  3. c3 with a: 5
  4. c3 with b: 8
  5. c4 with a: 5
  6. c4 with b: 8

huangapple
  • 本文由 发表于 2021年11月11日 00:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/69916911.html
匿名

发表评论

匿名网友

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

确定