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

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

How to pass fields of a struct to a function?

问题

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

import (
	"fmt"
	"errors"
)

type A struct {
	id    int
	value int
}

func findById(as []A, i int) (*A, error) {
    for _, a := range as {
	    if a.id == i {
    		return &a, nil
    	}
    }
    return nil, errors.New("no such item")
}

func findByValue(as []A, i int) (*A, error) {
    for _, a := range as {
	    if a.value == i {
    		return &a, nil
    	}
    }
    return nil, errors.New("no such item")
}
英文:

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?

import (
"fmt"
"errors"
)

type A struct {
	id    int
	value int
}

func findById(as []A, i int) (*A, error) {
    for _, a := range as {
	    if a.id == i {
    		return &account, nil
    	}
    }
    return nil, errors.New("no such item")
}

func findByValue(as []A, i int) (A, error) {
    for _, a := range as {
	    if a.value == i {
    		return &account, nil
    	}
    }
    return nil, errors.New("no such item")
}

答案1

得分: 1

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

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

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

package main

import "fmt"
import "errors"

type A struct {
    id    int
    value int
}

func findById(a []A, id int) (*A, error) {
    for i := 0; i < len(a); i++ {
        if a[i].id == id {
            return &a[i], nil
        }
    }
    return nil, errors.New("no such item")
}

func findByValue(a []A, value int) (*A, error) {
    for i := 0; i < len(a); i++ {
        if a[i].value == value {
            return &a[i], nil
        }
    }
    return nil, errors.New("no such item")
}

func findByIdValue(a []A, id, value int) (*A, error) {
    for i := 0; i < len(a); i++ {
        if a[i].id == id && a[i].value == value {
            return &a[i], nil
        }
    }
    return nil, errors.New("no such item")
}

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

func main() {

    t := []A{A{1, 2}, A{3, 4}, A{5, 6}}

    a, err := findById(t, 3)
    if err == nil {
        fmt.Println(*a) // {3 4}
    }

    a, err = findByValue(t, 4)
    if err == nil {
        fmt.Println(*a) // {3 4}
    }

    a, err = findByIdValue(t, 3, 4)
    if err == nil {
        fmt.Println(*a) // {3 4}
    }

    a, err = findByA(t, A{3, 4})
    if err == nil {
        fmt.Println(*a) // {3 4}
    }
}

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

package main

import "fmt"

type A struct {
    id    int
    value int
}
type SA []A

// 返回切片中第一个匹配项的索引,如果切片中不存在该项,则返回-1。
func (t SA) find(a A) int {
    for i := 0; i < len(t); i++ {
        if t[i].id == a.id && t[i].value == a.value {
            return i
        }
    }
    return -1
}

func main() {
    t := SA{A{1, 2}, A{3, 4}, A{5, 6}}
    i := t.find(A{3, 4})
    if i == -1 {
        fmt.Println("no such item")
        return
    }
    fmt.Println("t[", i, "] =", t[i]) // t[ 1 ] = {3 4}
}

输出结果:

t[ 1 ] = {3 4}

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

package main

import "fmt"

type A struct {
    id    int
    value int
}

// 返回切片中第一个匹配项的索引,如果切片中不存在该项,则返回-1。
func find(t []A, a A) int {
    for i := 0; i < len(t); i++ {
        if t[i].id == a.id && t[i].value == a.value {
            return i
        }
    }
    return -1
}

func main() {
    t := []A{A{1, 2}, A{3, 4}, A{5, 6}}
    i := find(t, A{3, 4})
    if i == -1 {
        fmt.Println("no such item")
        return
    }
    fmt.Println("t[", i, "] =", t[i]) // t[ 1 ] = {3 4}
}

输出结果:

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

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

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

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

Try this working sample code :

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

package main
import &quot;fmt&quot;
import &quot;errors&quot;
type A struct {
id    int
value int
}
func findById(a []A, id int) (*A, error) {
for i := 0; i &lt; len(a); i++ {
if a[i].id == id {
return &amp;a[i], nil
}
}
return nil, errors.New(&quot;no such item&quot;)
}
func findByValue(a []A, value int) (*A, error) {
for i := 0; i &lt; len(a); i++ {
if a[i].value == value {
return &amp;a[i], nil
}
}
return nil, errors.New(&quot;no such item&quot;)
}
func findByIdValue(a []A, id, value int) (*A, error) {
for i := 0; i &lt; len(a); i++ {
if a[i].id == id &amp;&amp; a[i].value == value {
return &amp;a[i], nil
}
}
return nil, errors.New(&quot;no such item&quot;)
}
func findByA(a []A, item A) (*A, error) {
for i := 0; i &lt; len(a); i++ {
if a[i].id == item.id &amp;&amp; a[i].value == item.value {
return &amp;a[i], nil
}
}
return nil, errors.New(&quot;no such item&quot;)
}
func main() {
t := []A{A{1, 2}, A{3, 4}, A{5, 6}}
a, err := findById(t, 3)
if err == nil {
fmt.Println(*a) // {3 4}
}
a, err = findByValue(t, 4)
if err == nil {
fmt.Println(*a) // {3 4}
}
a, err = findByIdValue(t, 3, 4)
if err == nil {
fmt.Println(*a) // {3 4}
}
a, err = findByA(t, A{3, 4})
if err == nil {
fmt.Println(*a) // {3 4}
}
}

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

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

package main
import &quot;fmt&quot;
type A struct {
id    int
value int
}
type SA []A
//it returns the index of the first instance of item  in slice, or -1 if item is not present in slice.
func (t SA) find(a A) int {
for i := 0; i &lt; len(t); i++ {
if t[i].id == a.id &amp;&amp; t[i].value == a.value {
return i
}
}
return -1
}
func main() {
t := SA{A{1, 2}, A{3, 4}, A{5, 6}}
i := t.find(A{3, 4})
if i == -1 {
fmt.Println(&quot;no such item&quot;)
return
}
fmt.Println(&quot;t[&quot;, i, &quot;] =&quot;, t[i]) // t[ 1 ] = {3 4}
}

output:

t[ 1 ] = {3 4}

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

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

package main
import &quot;fmt&quot;
type A struct {
id    int
value int
}
//it returns the index of the first instance of item  in slice, or -1 if item is not present in slice.
func find(t []A, a A) int {
for i := 0; i &lt; len(t); i++ {
if t[i].id == a.id &amp;&amp; t[i].value == a.value {
return i
}
}
return -1
}
func main() {
t := []A{A{1, 2}, A{3, 4}, A{5, 6}}
i := find(t, A{3, 4})
if i == -1 {
fmt.Println(&quot;no such item&quot;)
return
}
fmt.Println(&quot;t[&quot;, i, &quot;] =&quot;, t[i]) // t[ 1 ] = {3 4}
}

output:

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:

确定