go语言接口的使用

interface是一种类型 。一个对象的行为规范,只定义规范不实现,由具体的对象来实现规范的细节。

 

 

工厂方法模式:

一种类型多种接口

type sayer interface {
	 say ()
}

type  mover  interface {
	move()
}

type  fooder interface {
	food()
}

type   dog struct {
	name string
}

func (d dog )  say()  {
	fmt.Println("说话:",d.name)
}

func  (d dog) move(){
	fmt.Println("移动: ",d.name)
}


func (d * dog) food (){
	  fmt.Println( "食物:",d.name)
}


func main() {
	var x sayer
	var y mover
	var z  fooder

	a := dog{name: "hello",}
	x = a
	x.say()

	b := dog{name: "shanghai",}
	y = b
	y.move()

	c := dog{name: "gouliang",}
   z = &c
   z.food()
}

多种类型一种接口:

 type  mover interface {
 	Move()
 }

 type  dog struct {
 }

 type  car struct {

 }
 func (d dog) Move(){
    fmt.Println("百公里加速,只要一碗饭")
 }

 func (c car ) Move(){
	 fmt.Println("百公里加速,只要一箱油")
 }
func main() {
	var x mover
	x  = dog{}
	x.Move()
	x = car{}
	x.Move()
}

 接口可以使用断言来进行判断:

func justifyType(x interface{}){
	switch v := x.(type){
	case string:
		fmt.Printf("x is a string,value is %v\n", v)
	case int:
		fmt.Printf("x is a int is %v\n", v)
	case bool:
		fmt.Printf("x is a bool is %v\n", v)
	default:
		fmt.Println("unsupport type!")
	}
}

空接口判断:

对比有值的

 

 

抽象工厂模式:

1.游戏颜色:

// 颜色接口
type Color interface {
	Color() string
}

// 粉红色主题颜色
type PinkColor struct {
}

func (p *PinkColor) Color() string {
	return "pink"
}

// 蓝色主题颜色
type BlueColor struct {
}

func (b *BlueColor) Color() string {
	return "blue"
}

2.游戏声音:

// 声音接口
type Voice interface {
	Voice() string
}

// 女孩声音
type GirlVoice struct {
}

func (g *GirlVoice) Voice() string {
	return "girl"
}

// 男孩声音
type BoyVoice struct {
}

func (b *BoyVoice) Voice() string {
	return "boy"
}

3.不同主题:认真看,这里面的返回值很有趣

type ThemeFactory interface {
	GetVoice() Voice
	GetColor() Color
}

// 女生主题
type GirlTheme struct {
}

func (g *GirlTheme) GetVoice() Voice {
	return new(GirlVoice)
}

func (g *GirlTheme) GetColor() Color {
	return new(PinkColor)
}

// 男生主题
type BoyTheme struct {
}

func (b *BoyTheme) GetVoice() Voice {
	return new(BoyVoice)
}

func (b *BoyTheme) GetColor() Color {
	return new(BlueColor)
}

执行看一下效果吧:

func main() {

	girlTheme := new(GirlTheme)
	if girlTheme.GetColor().Color() != "pink" || girlTheme.GetVoice().Voice() != "girl" {
		fmt.Println("GirlTheme error")
	}

	// 实例化男生主题
	boyTheme := new(BoyTheme)
	if boyTheme.GetColor().Color() != "blue" || boyTheme.GetVoice().Voice() != "boy" {
		fmt.Println("BoyTheme error")
	}

	fmt.Println("以上函数的不等式都成立,不信可以把 != 换成 ==")
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你写CURD的接口。以下是使用GO语言编写的CURD接口的伪代码: 1. 导入所需的包和库 ``` import ( "encoding/json" "fmt" "net/http" "strconv" ) ``` 2. 定义一个结构体来表示数据模型 ``` type User struct { ID int `json:"id"` Name string `json:"name"` Age int `json:"age"` Address string `json:"address"` } ``` 3. 编写处理函数来处理请求,并提供响应 ``` func main() { http.HandleFunc("/users", handleUsers) http.ListenAndServe(":8080", nil) } func handleUsers(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": getUsers(w, r) case "POST": createUser(w, r) case "PUT": updateUser(w, r) case "DELETE": deleteUser(w, r) default: http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed) return } } ``` 4. 编写获取用户列表的函数 ``` func getUsers(w http.ResponseWriter, r *http.Request) { users := []User{ {1, "Alice", 25, "123 Main St"}, {2, "Bob", 30, "456 Elm St"}, {3, "Charlie", 35, "789 Oak St"}, } json.NewEncoder(w).Encode(users) } ``` 5. 编写创建用户的函数 ``` func createUser(w http.ResponseWriter, r *http.Request) { var user User err := json.NewDecoder(r.Body).Decode(&user) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } // Save user to database json.NewEncoder(w).Encode(user) } ``` 6. 编写更新用户的函数 ``` func updateUser(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.URL.Query().Get("id")) if err != nil { http.Error(w, "Invalid user ID.", http.StatusBadRequest) return } var user User err = json.NewDecoder(r.Body).Decode(&user) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } // Update user in database json.NewEncoder(w).Encode(user) } ``` 7. 编写删除用户的函数 ``` func deleteUser(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.URL.Query().Get("id")) if err != nil { http.Error(w, "Invalid user ID.", http.StatusBadRequest) return } // Remove user from database w.WriteHeader(http.StatusNoContent) } ``` 以上就是一个简单的CURD接口的实现。希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值