如何在Go中将结构体作为函数参数传递?

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

How to pass struct as a function argument in Go?

问题

package main

import "fmt"

type Person struct {
    name   string
    age    int
    job    string
    salary int
}

func test(class Person) {
    // Access and print Pers1 info
    fmt.Println("Name: ", class.name)
    fmt.Println("Age: ", class.age)
    fmt.Println("Job: ", class.job)
    fmt.Println("Salary: ", class.salary)
}

func main() {
    var pers1 Person
    var pers2 Person
    // Pers1 specification
    pers1.name = "Hege"
    pers1.age = 45
    pers1.job = "Teacher"
    pers1.salary = 6000
    // Pers2 specification
    pers2.name = "Cecilie"
    pers2.age = 24
    pers2.job = "Marketing"
    pers2.salary = 4500

    test(pers1) // Call the test function with pers1 as argument
}

这是你的代码。你想将整个结构体作为参数传递给函数test。你可以通过以下方式实现:

  1. 在函数test的参数中指定结构体类型:func test(class Person)
  2. main函数中调用test函数,并将pers1作为参数传递:test(pers1)

这样,test函数就可以访问和打印pers1的信息了。

英文:
package main

import "fmt"

type Person struct {
	name   string
	age    int
	job    string
	salary int
}

func test(class Person) {
	// Access and print Pers1 info
	fmt.Println("Name: ", class.name)
	fmt.Println("Age: ", class.age)
	fmt.Println("Job: ", class.job)
	fmt.Println("Salary: ", class.salary)
	
}
func main() {
	var pers1 Person
	var pers2 Person
	// Pers1 specification
	pers1.name = "Hege"
	pers1.age = 45
	pers1.job = "Teacher"
	pers1.salary = 6000
	// Pers2 specification
	pers2.name = "Cecilie"
	pers2.age = 24
	pers2.job = "Marketing"
	pers2.salary = 4500

}

/* This is my code. I want to pass whole struct to a function test as argument. But i don't know the syntax of like how can i achieve this. Kindly look into this and help me*/

答案1

得分: 2

你应该将其传递给函数调用,如 test(pers1)test(pers2)

英文:

You should pass it to function calling as test(pers1) and test(pers2)

答案2

得分: 0

包 main

import "fmt"

type Person struct {
name string
age int
job string
salary int
}

func test(class Person) {
// 访问并打印 Pers1 的信息
fmt.Println("姓名:", class.name)
fmt.Println("年龄:", class.age)
fmt.Println("工作:", class.job)
fmt.Println("薪水:", class.salary)

}
func main() {
var pers1 Person
var pers2 Person
// Pers1 的规格
pers1.name = "Hege"
pers1.age = 45
pers1.job = "Teacher"
pers1.salary = 6000
// Pers2 的规格
test(pers1)
pers2.name = "Cecilie"
pers2.age = 24
pers2.job = "Marketing"
pers2.salary = 4500
test(pers2)

}
/* 尝试一下,你必须将其传递为 test(pers1) 和 test(pers2)。希望现在能正常工作。*/

英文:
package main

import "fmt"

type Person struct {
	name   string
	age    int
	job    string
	salary int
}

func test(class Person) {
	// Access and print Pers1 info
	fmt.Println("Name: ", class.name)
	fmt.Println("Age: ", class.age)
	fmt.Println("Job: ", class.job)
	fmt.Println("Salary: ", class.salary)

}
func main() {
	var pers1 Person
	var pers2 Person
	// Pers1 specification
	pers1.name = "Hege"
	pers1.age = 45
	pers1.job = "Teacher"
	pers1.salary = 6000
	// Pers2 specification
	test(pers1)
	pers2.name = "Cecilie"
	pers2.age = 24
	pers2.job = "Marketing"
	pers2.salary = 4500
	test(pers2)

}

/* Try this you have to pass it as test(pers1) and test(pers2). I hope it works fine now.*/

huangapple
  • 本文由 发表于 2022年9月22日 01:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/73805026.html
匿名

发表评论

匿名网友

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

确定