From c885af3672fd4ce5a4c4433dbe6260a380540c10 Mon Sep 17 00:00:00 2001 From: yanzuoguang Date: Mon, 6 Oct 2025 19:18:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(go):=20=E6=B7=BB=E5=8A=A0=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=B1=BB=E5=9E=8B=E5=92=8C=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0-=20=E6=96=B0=E5=A2=9E=20funcTypes.go=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=8C=E5=AE=9A=E4=B9=89=20calcFunc=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=8F=8A=E5=85=B6=E5=AE=9E=E7=8E=B0=20-=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20funcTypes1.go=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8=E5=8C=BF=E5=90=8D=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=AE=A1=E7=AE=97=E5=99=A8=E9=80=BB=E8=BE=91?= =?UTF-8?q?=20-=20=E4=BF=AE=E6=94=B9=20main.go=20=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=9A=84=20main1=20=E5=92=8C=20main2=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0-=20=E5=88=9D=E5=A7=8B=E5=8C=96=20structs=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E7=9A=84=20go.mod=20=E6=96=87=E4=BB=B6=20-?= =?UTF-8?q?=20=E6=81=A2=E5=A4=8D=20structs/main.go=20=E5=8E=9F=E5=A7=8B?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E5=B9=B6=E4=BF=9D=E7=95=99=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 09-FuncAndTypes/funcTypes/funcTypes.go | 34 +++++++++++++++++++++++++ 09-FuncAndTypes/funcTypes/funcTypes1.go | 30 ++++++++++++++++++++++ 09-FuncAndTypes/funcTypes/main.go | 21 +++------------ 10-Structs/structs/go.mod | 3 +++ 10-Structs/structs/main.go | 21 +++++++++++++++ 5 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 09-FuncAndTypes/funcTypes/funcTypes.go create mode 100644 09-FuncAndTypes/funcTypes/funcTypes1.go create mode 100644 10-Structs/structs/go.mod create mode 100644 10-Structs/structs/main.go diff --git a/09-FuncAndTypes/funcTypes/funcTypes.go b/09-FuncAndTypes/funcTypes/funcTypes.go new file mode 100644 index 0000000..d16e93a --- /dev/null +++ b/09-FuncAndTypes/funcTypes/funcTypes.go @@ -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)) + } +} diff --git a/09-FuncAndTypes/funcTypes/funcTypes1.go b/09-FuncAndTypes/funcTypes/funcTypes1.go new file mode 100644 index 0000000..81f8f01 --- /dev/null +++ b/09-FuncAndTypes/funcTypes/funcTypes1.go @@ -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)) + } +} diff --git a/09-FuncAndTypes/funcTypes/main.go b/09-FuncAndTypes/funcTypes/main.go index df6b8ab..f74e2b5 100644 --- a/09-FuncAndTypes/funcTypes/main.go +++ b/09-FuncAndTypes/funcTypes/main.go @@ -1,21 +1,6 @@ package main -import ( - "fmt" -) - -//TIP

To run your code, right-click the code and select Run.

Alternatively, click -// the icon in the gutter and select the Run menu item from here.

- func main() { - //TIP

Press when your caret is at the underlined text - // to see how GoLand suggests fixing the warning.

Alternatively, if available, click the lightbulb to view possible fixes.

- s := "gopher" - fmt.Printf("Hello and welcome, %s!\n", s) - - for i := 1; i <= 5; i++ { - //TIP

To start your debugging session, right-click your code in the editor and select the Debug option.

We have set one breakpoint - // for you, but you can always add more by pressing .

- fmt.Println("i =", 100/i) - } -} \ No newline at end of file + main1() + main2() +} diff --git a/10-Structs/structs/go.mod b/10-Structs/structs/go.mod new file mode 100644 index 0000000..623f8b5 --- /dev/null +++ b/10-Structs/structs/go.mod @@ -0,0 +1,3 @@ +module structs + +go 1.22 diff --git a/10-Structs/structs/main.go b/10-Structs/structs/main.go new file mode 100644 index 0000000..df6b8ab --- /dev/null +++ b/10-Structs/structs/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" +) + +//TIP

To run your code, right-click the code and select Run.

Alternatively, click +// the icon in the gutter and select the Run menu item from here.

+ +func main() { + //TIP

Press when your caret is at the underlined text + // to see how GoLand suggests fixing the warning.

Alternatively, if available, click the lightbulb to view possible fixes.

+ s := "gopher" + fmt.Printf("Hello and welcome, %s!\n", s) + + for i := 1; i <= 5; i++ { + //TIP

To start your debugging session, right-click your code in the editor and select the Debug option.

We have set one breakpoint + // for you, but you can always add more by pressing .

+ fmt.Println("i =", 100/i) + } +} \ No newline at end of file