-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.go
75 lines (59 loc) · 1.3 KB
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"log"
"net/http"
"testing"
)
type PlayerModule interface {
DecreaseCoins(num int)
}
func DecreaseCoins(num int) {
fmt.Println("I eat bone")
}
//定义一个接口,接口内有eat和run两个方法
type Animal interface {
Eat()
Run()
}
//定义一个dog实体类,实现Animal接口
type Dog struct {
}
func (dog *Dog) Eat() {
fmt.Println("I eat bone")
}
func (dog *Dog) Run() {
fmt.Println("I run very fast")
}
func Hello(res http.ResponseWriter, req *http.Request) {
fmt.Fprintln(res, getHelloMsg())
}
func getHelloMsg() string {
log.Println("getHelloMsg ", "aa")
return "Let's Shopping!"
}
func testGetHelloMsg(t *testing.T) string {
msg := getHelloMsg()
if msg != "Let's Shopping!" {
t.Error("Get hello message error")
}
return "OK"
}
func main() {
// var (
// Player PlayerModule
// )
var a Animal
a = &Dog{}
a.Eat()
a.Run()
fmt.Println("hello world!!!")
http.HandleFunc("/", Hello) //注册URI路径与相应的处理函数
fmt.Println("Start listening...")
err := http.ListenAndServe(":4040", nil) // 监听9090端口,就跟javaweb中tomcat用的8080差不多一个意思吧
if err != nil {
panic(err)
log.Fatal("ListenAndServe: ", err)
}
// http://localhost:4040/
}