export

package module
v0.0.0-...-30e239e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2017 License: MIT Imports: 14 Imported by: 0

README

export

A lite package for export variable.

Install

go install github.com/vizee/export

Usage

package main

import (
    "net"

    "github.com/vizee/export"
)

type StrVar string

func (v *StrVar) Get() []byte {
    return []byte(*v)
}

func (v *StrVar) Set(s string) []byte {
    o := []byte(*v)
    *v = StrVar(s)
    return o
}

func main() {
    var (
        b = true
        i = 126
        s = "str"
        a StrVar
    )
    export.Bool("bool", &b)
    export.Int("int", &i)
    export.String("str", &s)
    export.Any("any", &a)
    l, err := net.Listen("tcp", ":0")
    if err != nil {
        panic(err)
    }
    println(l.Addr().String())
    // > telnet address port
    for {
        conn, err := l.Accept()
        if err != nil {
            panic(err)
        }
        err = export.Serve(conn, conn)
        conn.Close()
        if err != nil {
            println(err)
        }
    }
}

Commands

  • KEYS
list all keys
  • GET
get value by key
  • SET
update value by key and return previous value

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any(name string, v Var)

Any export a Var by name.

func Bool

func Bool(name string, v *bool)

Bool export a boolean variable by name.

func Int

func Int(name string, v *int)

Int export an integer variable by name.

func Int32

func Int32(name string, v *int32)

Int32 export an 32-bit integer variable by name.

func Int64

func Int64(name string, v *int64)

Int64 export an 64-bit integer variable by name.

func ReadOnly

func ReadOnly(name string, v string)

ReadOnly export a readonly string variable by name.

func Register

func Register(cmd string, fn func([]string) []byte)

Register a command handler or delete command if fn is nil.

func Serve

func Serve(r io.Reader, w io.Writer) error

Serve processes texted command from r, and writes the result to w.

func String

func String(name string, v *string)

String export a string variable by name.

Types

type GetOnlyFunc

type GetOnlyFunc func() []byte

GetOnlyFunc type use a function as export.Var but no Set.

func (GetOnlyFunc) Get

func (fn GetOnlyFunc) Get() []byte

Get calls function.

func (GetOnlyFunc) Set

func (fn GetOnlyFunc) Set(string) []byte

Set always return nil.

type Var

type Var interface {
	Get() []byte
	Set(v string) []byte
}

Var is an abstract type for exported variable.

type VarFunc

type VarFunc func(string) []byte

VarFunc type use a function as export.Var.

func (VarFunc) Get

func (fn VarFunc) Get() []byte

Get calls function with an empty string.

func (VarFunc) Set

func (fn VarFunc) Set(v string) []byte

Set calls function with SET argument.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL