深入理解Golang中的函数机制

深入理解Golang中的函数机制 我需要帮助理解如何让以下代码正常工作:

package main

import "fmt"

type compensation struct {
	salary        int
	insurance     bool
	stocksOptions bool
}

type employee struct {
	name      string
	id        int
	totalComp *compensation
}

func main() {
	newEmployee := employee{
		name: "gary",
		id:   1,
		totalComp: &compensation{
			salary:        100,
			insurance:     true,
			stocksOptions: true,
		},
	}

	var anotherNewEmployee = new(employee)
	anotherNewEmployee.id = 2
	anotherNewEmployee.name = "rich"
	anotherNewEmployee.totalComp.insurance = false

	fmt.Println(anotherNewEmployee)
	anotherNewEmployee.totalComp.insurance = true
	fmt.Println(anotherNewEmployee.totalComp.insurance)

	fmt.Println(newEmployee.totalComp.salary)
	newEmployee.totalComp.salary = 4
	fmt.Println(newEmployee.totalComp.salary)
}

当我尝试设置 anotherNewEmployee.totalComp.insurance = false 时,我遇到了一个错误,我不确定我理解如何给它赋值。


更多关于深入理解Golang中的函数机制的实战教程也可以访问 https://www.itying.com/category-94-b0.html

6 回复

那是一些用于说明问题的伪代码。

更多关于深入理解Golang中的函数机制的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


petrus:

anotherNewEmployee.totalComp.insurance = false

完美,感谢您的解释!

NobbZ:

执行 ((*compensation)nil).insurance = false

syntax error: unexpected nil, expecting )

在尝试修改 compensation 之前,你还没有创建它。所以你实际上是在尝试执行 ((*compensation)nil).insurance = false

((*compensation)nil).insurance = false

anotherNewEmployee.totalCompnil,因此添加 anotherNewEmployee.totalComp = new(compensation)

package main

import "fmt"

type compensation struct {
	salary        int
	insurance     bool
	stocksOptions bool
}

type employee struct {
	name      string
	id        int
	totalComp *compensation
}

func main() {
	newEmployee := employee{
		name: "gary",
		id:   1,
		totalComp: &compensation{
			salary:        100,
			insurance:     true,
			stocksOptions: true,
		},
	}

	var anotherNewEmployee = new(employee)
	anotherNewEmployee.id = 2
	anotherNewEmployee.name = "rich"
	anotherNewEmployee.totalComp = new(compensation)
	anotherNewEmployee.totalComp.insurance = false

	fmt.Println(anotherNewEmployee)
	anotherNewEmployee.totalComp.insurance = true
	fmt.Println(anotherNewEmployee.totalComp.insurance)

	fmt.Println(newEmployee.totalComp.salary)
	newEmployee.totalComp.salary = 4
	fmt.Println(newEmployee.totalComp.salary)
}

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

&{rich 2 0xc00002c040}
true
100
4

你的代码在 anotherNewEmployee.totalComp.insurance = false 处会出现 panic,因为 anotherNewEmployee.totalCompnil。使用 new(employee) 创建结构体时,其指针字段 totalComp 默认值为 nil,直接访问其字段会导致运行时错误。

以下是修正后的代码:

package main

import "fmt"

type compensation struct {
	salary        int
	insurance     bool
	stocksOptions bool
}

type employee struct {
	name      string
	id        int
	totalComp *compensation
}

func main() {
	newEmployee := employee{
		name: "gary",
		id:   1,
		totalComp: &compensation{
			salary:        100,
			insurance:     true,
			stocksOptions: true,
		},
	}

	var anotherNewEmployee = new(employee)
	anotherNewEmployee.id = 2
	anotherNewEmployee.name = "rich"
	anotherNewEmployee.totalComp = &compensation{} // 初始化指针
	anotherNewEmployee.totalComp.insurance = false

	fmt.Println(anotherNewEmployee)
	anotherNewEmployee.totalComp.insurance = true
	fmt.Println(anotherNewEmployee.totalComp.insurance)

	fmt.Println(newEmployee.totalComp.salary)
	newEmployee.totalComp.salary = 4
	fmt.Println(newEmployee.totalComp.salary)
}

关键修改是添加了 anotherNewEmployee.totalComp = &compensation{} 来初始化指针字段。这样 anotherNewEmployee.totalComp 就不再是 nil,可以安全访问其字段。

另一种初始化方式是使用复合字面量:

anotherNewEmployee := &employee{
	name: "rich",
	id:   2,
	totalComp: &compensation{
		insurance: false,
	},
}

这两种方式都确保 totalComp 指针指向有效的 compensation 结构体实例。

回到顶部