Skip to content

Commit

Permalink
修改
Browse files Browse the repository at this point in the history
  • Loading branch information
spectrelb committed Jul 16, 2020
1 parent 549a150 commit 65909c7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 20200716/goroutines.go
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)
}
}
54 changes: 54 additions & 0 deletions 20200716/interfaces.go
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)
}
27 changes: 27 additions & 0 deletions 20200716/methods.go
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()
}

0 comments on commit 65909c7

Please sign in to comment.