当前位置:   article > 正文

go开源网络库nano(8)-组件_golang nano教程

golang nano教程

前言

package component

组件

定义


//Component  组件的接口
type Component interface {
	Init()
	AfterInit()
	BeforeShutdown()
	Shutdown()
}
// base 是基本的,默认的实现
type Base struct{}
// Init was called to initialize the component.
func (c *Base) Init() {}
// AfterInit was called after the component is initialized.
func (c *Base) AfterInit() {}
// BeforeShutdown was called before the component to shutdown.
func (c *Base) BeforeShutdown() {}
// Shutdown was called to shutdown the component.
func (c *Base) Shutdown() {}

//Option
options struct {
	name      string              // component name
	nameFunc  func(string) string // rename handler name
	schedName string              // schedName name
}
// Option used to customize handler  对options结构体进行设置的函数模板
Option func(options *options)
      

type CompWithOptions struct {
	Comp Component   
	Opts []Option //对options进行设置的函数组
}

type Components struct {
	comps []CompWithOptions
}

  • 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

Components 是总组件,可以接受多个组件的注册。 每个组件又可以提供不同的逻辑处理。
Component 是接口,base是实现接口的一个简单的结构,默认初始化用。

方法

1. 注册

// Register registers a component to hub with options
func (cs *Components) Register(c Component, options ...Option) {
	cs.comps = append(cs.comps, CompWithOptions{c, options})
}

  • 1
  • 2
  • 3
  • 4
  • 5

2.获取组件列表

// List returns all components with it's options
func (cs *Components) List() []CompWithOptions {
	return cs.comps
}

  • 1
  • 2
  • 3
  • 4
  • 5

举个例子demo

//demo.go
package demo

import (
	"github.com/lonng/nano/component"
	"github.com/lonng/nano/session"
	"log"
)

type Demo struct {
	component.Base
	sq int64
}

func newDemo() *Demo {
	return &Demo{}
}


func (ds *Demo) Dprint(s *session.Session, msg []byte) error {
	log.Println("hello,Demo!")
	return nil
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
//demo_init.go
package demo
var (
	// All services in master server
	Services = &component.Components{}

	demoService = newdemoService()
)

func init() {
	Services.Register(demoService )
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
// main.go
package main

import (
	"github.com/lonng/nano"
	"github.com/lonng/nano/examples/myDemo/demo"
)

func main() {

	listen := "127.0.0.1:10000"

	nano.Listen(listen,
		nano.WithMaster(),
		nano.WithComponents(demo.Services),
		nano.WithDebugMode(),
	)

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

执行后的结构:
在这里插入图片描述

在编译的时候出现个问题:
type Demo struct{}
结构名称要大写,否则编译不过去。

下一章

下一章继续关于组件的讨论,组件会变成服务,而服务提供具体方法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/624133
推荐阅读
相关标签
  

闽ICP备14008679号