golang信号SIGSEGV:分段违规

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

golang signal SIGSEGV: segmentation violation

问题

我正在尝试在Go语言中使用高阶函数。在包含"result := calcFunction(radius)"的行中,printResult函数发生了panic。有人可以帮我看看我在这里做错了什么吗?

package main

import "fmt"

func calcArea(r float64) float64 {
	return 3.14 * r * r
}

func calcPerimeter(r float64) float64 {
	return 2 * 3.14 * r
}

func calcDiameter(r float64) float64 {
	return 2 * r
}

func getFunction(query int) func(r float64) float64 {
	query_to_func := map[int]func(r float64) float64{
		1: calcArea,
		2: calcPerimeter,
		3: calcDiameter,
	}
	return query_to_func[query]
}

func printResult(radius float64, calcFunction func(r float64) float64) {
	result := calcFunction(radius)
	fmt.Println("Result:", result)
}

func main() {
	var query int
	var radius float64

	fmt.Print("Enter the radius of the circle: ")
	fmt.Scanf("%f", &radius)
	fmt.Print("Enter operation: \n 1: Area\n 2: Perimeter\n 3: Diameter \n :")
	fmt.Scanf("%d", &query)
	printResult(radius, getFunction(query))
}

你可以尝试将fmt.Scanf("%f", &query)改为fmt.Scanf("%d", &query),因为query是一个整数,而不是浮点数。这可能是导致panic的原因。

英文:

I am trying higher order function in golang. It is panicking in printResult function at line containing "result := calcFunction(radius)". Can anybody help me as what I am doing wrong here.

package main

import "fmt"

func calcArea(r float64) float64 {
	return 3.14 * r * r
}

func calcPerimeter(r float64) float64 {
	return 2 * 3.14 * r
}

func calcDiameter(r float64) float64 {
	return 2 * r
}

func getFunction(query int) func(r float64) float64 {

	query_to_func := map[int]func(r float64) float64{
		1: calcArea,
		2: calcPerimeter,
		3: calcDiameter,
	}
	return query_to_func[query]

}

func printResult(radius float64, calcFunction func(r float64) float64) {
	result := calcFunction(radius)
	fmt.Println("Result:", result)
}

func main() {
	var query int
	var radius float64

	fmt.Print("Enter the radius of the circle: ")
	fmt.Scanf("%f", &radius)
	fmt.Print("Enter operation: \n 1: Area\n 2: Perimeter\n 3: Diameter \n :")
	fmt.Scanf("%f", &query)
	printResult(radius, getFunction(query))

}

答案1

得分: 3

问题出在fmt.Scanf("%f", &query)这一行。query是int类型,而你正在使用%f(float64)进行扫描。因此,值变为0。由于映射中没有该值,所以会引发错误。

func main() {
    var query int
    var radius float64

    fmt.Print("Enter the radius of the circle: ")
    fmt.Scanf("%f", &radius)
    fmt.Print("Enter operation: \n 1: Area\n 2: Perimeter\n 3: Diameter \n :")
    fmt.Scanf("%d", &query)
    printResult(radius, getFunction(query))
}
英文:

Problem is with the fmt.Scanf("%f", &query). query is int and you are scanning to %f(float64). So the value become 0. Since map does not have that value, it panics.

func main() {
	var query int
	var radius float64

	fmt.Print("Enter the radius of the circle: ")
	fmt.Scanf("%f", &radius)
	fmt.Print("Enter operation: \n 1: Area\n 2: Perimeter\n 3: Diameter \n :")
	fmt.Scanf("%d", &query)
	printResult(radius, getFunction(query))
}

</details>



huangapple
  • 本文由 发表于 2022年3月14日 12:59:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/71463164.html
匿名

发表评论

匿名网友

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

确定