feat(go): 添加函数类型和计算器实现- 新增 funcTypes.go 文件,定义 calcFunc 类型及其实现
- 新增 funcTypes1.go 文件,使用匿名函数实现计算器逻辑 - 修改 main.go 调用新增的 main1 和 main2 函数- 初始化 structs 模块的 go.mod 文件 - 恢复 structs/main.go 原始内容并保留调试提示注释
This commit is contained in:
34
09-FuncAndTypes/funcTypes/funcTypes.go
Normal file
34
09-FuncAndTypes/funcTypes/funcTypes.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type calcFunc func(float64) float64
|
||||
|
||||
func calcWithTax(price float64) float64 {
|
||||
return price + (price * 0.2)
|
||||
}
|
||||
|
||||
func calcWithoutTax(price float64) float64 {
|
||||
return price
|
||||
}
|
||||
|
||||
func printPrice(product string, price float64, calculator calcFunc) {
|
||||
fmt.Println("Product:", product, "Price:", calculator(price))
|
||||
}
|
||||
|
||||
func selectCalculator(price float64) calcFunc {
|
||||
if price > 100 {
|
||||
return calcWithTax
|
||||
}
|
||||
return calcWithoutTax
|
||||
}
|
||||
|
||||
func main1() {
|
||||
products := map[string]float64{
|
||||
"Kayak": 275,
|
||||
"LifeJacket": 48.95,
|
||||
}
|
||||
for product, price := range products {
|
||||
printPrice(product, price, selectCalculator(price))
|
||||
}
|
||||
}
|
||||
30
09-FuncAndTypes/funcTypes/funcTypes1.go
Normal file
30
09-FuncAndTypes/funcTypes/funcTypes1.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func printPrice2(product string, price float64, calculator calcFunc) {
|
||||
fmt.Println("Product:", product, "Price:", calculator(price))
|
||||
}
|
||||
|
||||
func selectCalculator2(price float64) calcFunc {
|
||||
if price > 100 {
|
||||
var withTax calcFunc = func(price float64) float64 {
|
||||
return price + (price * 0.2)
|
||||
}
|
||||
return withTax
|
||||
}
|
||||
var withoutTax calcFunc = func(price float64) float64 {
|
||||
return price
|
||||
}
|
||||
return withoutTax
|
||||
}
|
||||
|
||||
func main2() {
|
||||
products := map[string]float64{
|
||||
"Kayak": 275,
|
||||
"LifeJacket": 48.95,
|
||||
}
|
||||
for product, price := range products {
|
||||
printPrice2(product, price, selectCalculator2(price))
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//TIP <p>To run your code, right-click the code and select <b>Run</b>.</p> <p>Alternatively, click
|
||||
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.</p>
|
||||
|
||||
func main() {
|
||||
//TIP <p>Press <shortcut actionId="ShowIntentionActions"/> when your caret is at the underlined text
|
||||
// to see how GoLand suggests fixing the warning.</p><p>Alternatively, if available, click the lightbulb to view possible fixes.</p>
|
||||
s := "gopher"
|
||||
fmt.Printf("Hello and welcome, %s!\n", s)
|
||||
|
||||
for i := 1; i <= 5; i++ {
|
||||
//TIP <p>To start your debugging session, right-click your code in the editor and select the Debug option.</p> <p>We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.</p>
|
||||
fmt.Println("i =", 100/i)
|
||||
}
|
||||
}
|
||||
main1()
|
||||
main2()
|
||||
}
|
||||
|
||||
3
10-Structs/structs/go.mod
Normal file
3
10-Structs/structs/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module structs
|
||||
|
||||
go 1.22
|
||||
21
10-Structs/structs/main.go
Normal file
21
10-Structs/structs/main.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//TIP <p>To run your code, right-click the code and select <b>Run</b>.</p> <p>Alternatively, click
|
||||
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.</p>
|
||||
|
||||
func main() {
|
||||
//TIP <p>Press <shortcut actionId="ShowIntentionActions"/> when your caret is at the underlined text
|
||||
// to see how GoLand suggests fixing the warning.</p><p>Alternatively, if available, click the lightbulb to view possible fixes.</p>
|
||||
s := "gopher"
|
||||
fmt.Printf("Hello and welcome, %s!\n", s)
|
||||
|
||||
for i := 1; i <= 5; i++ {
|
||||
//TIP <p>To start your debugging session, right-click your code in the editor and select the Debug option.</p> <p>We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
|
||||
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.</p>
|
||||
fmt.Println("i =", 100/i)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user