-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
func main() { | ||
//获取当前GOROOT目录 | ||
fmt.Println("GOROOT:", runtime.GOROOT()) | ||
//获取当前操作系统 | ||
fmt.Println("操作系统:", runtime.GOOS) | ||
fmt.Println("逻辑CPU数量:", runtime.NumCPU()) | ||
go testgo1() | ||
go testgo2() | ||
for i := 0; i < 5; i++ { | ||
fmt.Println(i) | ||
} | ||
|
||
time.Sleep(3000 * time.Millisecond) | ||
fmt.Println("main 函数结束") | ||
} | ||
|
||
func testgo1 () { | ||
for i := 0; i < 10; i++ { | ||
fmt.Println("测试goroutine1",i) | ||
} | ||
} | ||
|
||
func testgo2 () { | ||
for i := 0; i < 10; i++ { | ||
fmt.Println("测试goroutine2",i) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
// 这里是一个几何体的基本接口。 | ||
type geometry interface { | ||
area() float64 | ||
perim() float64 | ||
} | ||
|
||
type rect struct { | ||
width, height float64 | ||
} | ||
|
||
type circle struct { | ||
radius float64 | ||
} | ||
|
||
// 要在 Go 中实现一个接口,我们就需要实现接口中的所有方法。 这里我们在 `rect` 上实现了 `geometry` 接口。 | ||
func (r rect) area() float64 { | ||
return r.width * r.height | ||
} | ||
|
||
func (r rect) perim() float64 { | ||
return 2 * r.width + 2 * r.height | ||
} | ||
|
||
func (c circle) area() float64 { | ||
return math.Pi * c.radius * c.radius | ||
} | ||
|
||
func (c circle) perim() float64 { | ||
return 2 * math.Pi * c.radius | ||
} | ||
|
||
// 如果一个变量具有接口类型,那么我们可以调用指定接口中的方法。 | ||
func measure(g geometry) { | ||
fmt.Println(g) | ||
fmt.Println(g.area()) | ||
fmt.Println(g.perim()) | ||
} | ||
|
||
func main() { | ||
r := rect{3, 4} | ||
c := circle{5} | ||
|
||
// 结构体类型 `circle` 和 `rect` 都实现了 `geometry` 接口, | ||
// 所以我们可以使用它们的实例作为 `measure` 的参数。 | ||
measure(r) | ||
measure(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Role struct { | ||
Name string | ||
Ability string | ||
Level int | ||
Kill float64 | ||
} | ||
|
||
func (r Role) Kungfu() { | ||
fmt.Printf("我是:%s,我的武功:%s,已经练到%d级了,杀伤力%.1f\n", r.Name, r.Ability, r.Level, r.Kill) | ||
} | ||
|
||
func (r *Role) Kungfu1() { | ||
fmt.Printf("我是:%s,我的武功:%s,已经练到%d级了,杀伤力%.1f\n", r.Name, r.Ability, r.Level, r.Kill) | ||
} | ||
|
||
func main() { | ||
role := Role{"1", "2", 3, 4.0} | ||
role.Kungfu() | ||
|
||
role1 := &Role{"1", "2", 3, 4.0} | ||
role1.Kungfu1() | ||
} | ||
|