如何将结构体的字段传递给函数?

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

How to pass fields of a struct to a function?

问题

我想知道是否可以将findByIdfindByValue合并为一个函数?即在现有参数的基础上,传递结构体中的字段?

  1. import (
  2. "fmt"
  3. "errors"
  4. )
  5. type A struct {
  6. id int
  7. value int
  8. }
  9. func findById(as []A, i int) (*A, error) {
  10. for _, a := range as {
  11. if a.id == i {
  12. return &a, nil
  13. }
  14. }
  15. return nil, errors.New("no such item")
  16. }
  17. func findByValue(as []A, i int) (*A, error) {
  18. for _, a := range as {
  19. if a.value == i {
  20. return &a, nil
  21. }
  22. }
  23. return nil, errors.New("no such item")
  24. }
英文:

I wonder if findById and findByValue can be combined to one function? That is to pass the field in the struct in addition to the existing params?

  1. import (
  2. "fmt"
  3. "errors"
  4. )
  5. type A struct {
  6. id int
  7. value int
  8. }
  9. func findById(as []A, i int) (*A, error) {
  10. for _, a := range as {
  11. if a.id == i {
  12. return &account, nil
  13. }
  14. }
  15. return nil, errors.New("no such item")
  16. }
  17. func findByValue(as []A, i int) (A, error) {
  18. for _, a := range as {
  19. if a.value == i {
  20. return &account, nil
  21. }
  22. }
  23. return nil, errors.New("no such item")
  24. }

答案1

得分: 1

如果你正在寻找func findByA(a []A, item A) (*A, error)的代码示例:

  1. func findByA(a []A, item A) (*A, error) {
  2. for i := 0; i < len(a); i++ {
  3. if a[i].id == item.id && a[i].value == item.value {
  4. return &a[i], nil
  5. }
  6. }
  7. return nil, errors.New("no such item")
  8. }

可以尝试以下工作示例代码:

  1. package main
  2. import "fmt"
  3. import "errors"
  4. type A struct {
  5. id int
  6. value int
  7. }
  8. func findById(a []A, id int) (*A, error) {
  9. for i := 0; i < len(a); i++ {
  10. if a[i].id == id {
  11. return &a[i], nil
  12. }
  13. }
  14. return nil, errors.New("no such item")
  15. }
  16. func findByValue(a []A, value int) (*A, error) {
  17. for i := 0; i < len(a); i++ {
  18. if a[i].value == value {
  19. return &a[i], nil
  20. }
  21. }
  22. return nil, errors.New("no such item")
  23. }
  24. func findByIdValue(a []A, id, value int) (*A, error) {
  25. for i := 0; i < len(a); i++ {
  26. if a[i].id == id && a[i].value == value {
  27. return &a[i], nil
  28. }
  29. }
  30. return nil, errors.New("no such item")
  31. }
  32. func findByA(a []A, item A) (*A, error) {
  33. for i := 0; i < len(a); i++ {
  34. if a[i].id == item.id && a[i].value == item.value {
  35. return &a[i], nil
  36. }
  37. }
  38. return nil, errors.New("no such item")
  39. }
  40. func main() {
  41. t := []A{A{1, 2}, A{3, 4}, A{5, 6}}
  42. a, err := findById(t, 3)
  43. if err == nil {
  44. fmt.Println(*a) // {3 4}
  45. }
  46. a, err = findByValue(t, 4)
  47. if err == nil {
  48. fmt.Println(*a) // {3 4}
  49. }
  50. a, err = findByIdValue(t, 3, 4)
  51. if err == nil {
  52. fmt.Println(*a) // {3 4}
  53. }
  54. a, err = findByA(t, A{3, 4})
  55. if err == nil {
  56. fmt.Println(*a) // {3 4}
  57. }
  58. }

如果你需要获取项目的索引,你可以使用接收器方法,像这个工作示例代码:

  1. package main
  2. import "fmt"
  3. type A struct {
  4. id int
  5. value int
  6. }
  7. type SA []A
  8. // 返回切片中第一个匹配项的索引,如果切片中不存在该项,则返回-1。
  9. func (t SA) find(a A) int {
  10. for i := 0; i < len(t); i++ {
  11. if t[i].id == a.id && t[i].value == a.value {
  12. return i
  13. }
  14. }
  15. return -1
  16. }
  17. func main() {
  18. t := SA{A{1, 2}, A{3, 4}, A{5, 6}}
  19. i := t.find(A{3, 4})
  20. if i == -1 {
  21. fmt.Println("no such item")
  22. return
  23. }
  24. fmt.Println("t[", i, "] =", t[i]) // t[ 1 ] = {3 4}
  25. }

输出结果:

  1. t[ 1 ] = {3 4}

如果你需要获取项目的索引,你可以使用函数,像这个工作示例代码:

  1. package main
  2. import "fmt"
  3. type A struct {
  4. id int
  5. value int
  6. }
  7. // 返回切片中第一个匹配项的索引,如果切片中不存在该项,则返回-1。
  8. func find(t []A, a A) int {
  9. for i := 0; i < len(t); i++ {
  10. if t[i].id == a.id && t[i].value == a.value {
  11. return i
  12. }
  13. }
  14. return -1
  15. }
  16. func main() {
  17. t := []A{A{1, 2}, A{3, 4}, A{5, 6}}
  18. i := find(t, A{3, 4})
  19. if i == -1 {
  20. fmt.Println("no such item")
  21. return
  22. }
  23. fmt.Println("t[", i, "] =", t[i]) // t[ 1 ] = {3 4}
  24. }

输出结果:

  1. t[ 1 ] = {3 4}
英文:

If you are looking for func findByA(a []A, item A) (*A, error):

<!-- language: lang-golang -->

  1. func findByA(a []A, item A) (*A, error) {
  2. for i := 0; i &lt; len(a); i++ {
  3. if a[i].id == item.id &amp;&amp; a[i].value == item.value {
  4. return &amp;a[i], nil
  5. }
  6. }
  7. return nil, errors.New(&quot;no such item&quot;)
  8. }

Try this working sample code :

<!-- language: lang-golang -->

  1. package main
  2. import &quot;fmt&quot;
  3. import &quot;errors&quot;
  4. type A struct {
  5. id int
  6. value int
  7. }
  8. func findById(a []A, id int) (*A, error) {
  9. for i := 0; i &lt; len(a); i++ {
  10. if a[i].id == id {
  11. return &amp;a[i], nil
  12. }
  13. }
  14. return nil, errors.New(&quot;no such item&quot;)
  15. }
  16. func findByValue(a []A, value int) (*A, error) {
  17. for i := 0; i &lt; len(a); i++ {
  18. if a[i].value == value {
  19. return &amp;a[i], nil
  20. }
  21. }
  22. return nil, errors.New(&quot;no such item&quot;)
  23. }
  24. func findByIdValue(a []A, id, value int) (*A, error) {
  25. for i := 0; i &lt; len(a); i++ {
  26. if a[i].id == id &amp;&amp; a[i].value == value {
  27. return &amp;a[i], nil
  28. }
  29. }
  30. return nil, errors.New(&quot;no such item&quot;)
  31. }
  32. func findByA(a []A, item A) (*A, error) {
  33. for i := 0; i &lt; len(a); i++ {
  34. if a[i].id == item.id &amp;&amp; a[i].value == item.value {
  35. return &amp;a[i], nil
  36. }
  37. }
  38. return nil, errors.New(&quot;no such item&quot;)
  39. }
  40. func main() {
  41. t := []A{A{1, 2}, A{3, 4}, A{5, 6}}
  42. a, err := findById(t, 3)
  43. if err == nil {
  44. fmt.Println(*a) // {3 4}
  45. }
  46. a, err = findByValue(t, 4)
  47. if err == nil {
  48. fmt.Println(*a) // {3 4}
  49. }
  50. a, err = findByIdValue(t, 3, 4)
  51. if err == nil {
  52. fmt.Println(*a) // {3 4}
  53. }
  54. a, err = findByA(t, A{3, 4})
  55. if err == nil {
  56. fmt.Println(*a) // {3 4}
  57. }
  58. }

If you need the index of item, you may use receiver method, like this working sample code:

<!-- language: lang-golang -->

  1. package main
  2. import &quot;fmt&quot;
  3. type A struct {
  4. id int
  5. value int
  6. }
  7. type SA []A
  8. //it returns the index of the first instance of item in slice, or -1 if item is not present in slice.
  9. func (t SA) find(a A) int {
  10. for i := 0; i &lt; len(t); i++ {
  11. if t[i].id == a.id &amp;&amp; t[i].value == a.value {
  12. return i
  13. }
  14. }
  15. return -1
  16. }
  17. func main() {
  18. t := SA{A{1, 2}, A{3, 4}, A{5, 6}}
  19. i := t.find(A{3, 4})
  20. if i == -1 {
  21. fmt.Println(&quot;no such item&quot;)
  22. return
  23. }
  24. fmt.Println(&quot;t[&quot;, i, &quot;] =&quot;, t[i]) // t[ 1 ] = {3 4}
  25. }

output:

  1. t[ 1 ] = {3 4}

If you need the index of item, you may use function, like this working sample code:

<!-- language: lang-golang -->

  1. package main
  2. import &quot;fmt&quot;
  3. type A struct {
  4. id int
  5. value int
  6. }
  7. //it returns the index of the first instance of item in slice, or -1 if item is not present in slice.
  8. func find(t []A, a A) int {
  9. for i := 0; i &lt; len(t); i++ {
  10. if t[i].id == a.id &amp;&amp; t[i].value == a.value {
  11. return i
  12. }
  13. }
  14. return -1
  15. }
  16. func main() {
  17. t := []A{A{1, 2}, A{3, 4}, A{5, 6}}
  18. i := find(t, A{3, 4})
  19. if i == -1 {
  20. fmt.Println(&quot;no such item&quot;)
  21. return
  22. }
  23. fmt.Println(&quot;t[&quot;, i, &quot;] =&quot;, t[i]) // t[ 1 ] = {3 4}
  24. }

output:

  1. t[ 1 ] = {3 4}

huangapple
  • 本文由 发表于 2016年8月23日 01:24:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/39085358.html
匿名

发表评论

匿名网友

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

确定