英文:
How to pass fields of a struct to a function?
问题
我想知道是否可以将findById
和findByValue
合并为一个函数?即在现有参数的基础上,传递结构体中的字段?
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 < 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")
}
Try this working sample code :
<!-- language: lang-golang -->
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}
}
}
If you need the index of item, you may use receiver method, like this working sample code:
<!-- language: lang-golang -->
package main
import "fmt"
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 < 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}
}
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 "fmt"
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 < 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}
}
output:
t[ 1 ] = {3 4}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论