本文整理匯總了Golang中github.com/fcavani/e.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ExportDel
func (i *Instances) ExportDel(sess, inst, method string, retval int) error {
i.lck.Lock()
defer i.lck.Unlock()
s, found := i.Insts[sess]
if !found {
return e.New(ErrSessionNotFound)
}
in, found := s.Insts[inst]
if !found {
return e.New(ErrInstanceNotFound)
}
retvals, found := in.Export()[method]
if !found {
return e.New(ErrMethodNotFound)
}
_, found = retvals[retval]
if !found {
return e.New(ErrReturnValueNotFound)
}
delete(retvals, retval)
if len(retvals) == 0 {
delete(in.Export(), method)
}
return nil
}
示例2: NewMongoDb
func NewMongoDb(rawurl, collection string, safe *mgo.Safe, entry Entry, timeout time.Duration) (Storer, error) {
var err error
m := &MongoDb{}
err = chkData(entry)
if err != nil {
return nil, e.Forward(err)
}
m.tentry = reflect.TypeOf(entry)
if m.tentry.Kind() != reflect.Ptr {
m.tentry = reflect.PtrTo(m.tentry)
}
m.session, err = mgo.DialWithTimeout(rawurl, timeout)
if err != nil {
return nil, e.New(err)
}
parsed, err := url.Parse(rawurl)
if err != nil {
return nil, e.New(err)
}
if safe == nil {
safe = &mgo.Safe{}
}
m.session.SetSafe(safe)
m.dbname = strings.Trim(parsed.Path, "/")
m.db = m.session.DB(m.dbname)
m.collection = collection
m.c = m.db.C(collection)
return m, nil
}
示例3: Init
func (c *clone) Init(inst string) error {
if c.Num <= 0 {
return e.New("number of clones invalid")
}
if c.Session == nil {
return e.New("nil Session")
}
if c.Num > c.MaxNum {
return e.New("number of initial streams is greater than the maximun")
}
c.inst = inst
c.once.Do(func() {
c.conns = make([]connInst, 0, c.Num)
log.ProtoLevel().Tag("gormethods", "client", "clone").Println("Init clones.")
for i := 0; i < c.Num; i++ {
conn, err := c.Session.NamedInstance(c.inst)
if err != nil {
log.ProtoLevel().Tag("gormethods", "client", "clone").Printf("New instance failed: %v.", err)
continue
}
c.conns = append(c.conns, conn)
}
})
return nil
}
示例4: Export
func (i *Instances) Export(sess, inst string, exp *ToExport) error {
i.lck.Lock()
defer i.lck.Unlock()
s, found := i.Insts[sess]
if !found {
return e.New(ErrSessionNotFound)
}
in, found := s.Insts[inst]
if !found {
return e.New(ErrInstanceNotFound)
}
m, ok := in.Instance().Type().MethodByName(exp.Method)
if !ok {
return e.New(ErrMethodNotFound)
}
if exp.RetvalPos >= m.Type.NumOut() {
return e.New(ErrInvRetval)
}
retvals, found := in.Export()[exp.Method]
if !found {
retvals = make(map[int]*Export)
in.Export()[exp.Method] = retvals
}
if exp.Export.Client == nil {
return e.New(ErrInvClient)
}
retvals[exp.RetvalPos] = exp.Export
return nil
}
示例5: NewMulti
//NewMulti creates a MultiLog
func NewMulti(vals ...interface{}) LogBackend {
if len(vals)%2 != 0 {
Fail(e.New("parameters must be in pair of LogBackend and Formatter"))
return nil
}
l := len(vals) / 2
mp := make([]LogBackend, 0, l)
for i := 0; i < len(vals); i += 2 {
bak, ok := vals[i].(LogBackend)
if !ok {
Fail(e.New("not a LogBackend"))
return nil
}
f, ok := vals[i+1].(Formatter)
if !ok {
Fail(e.New("not a Formatter"))
return nil
}
bak.F(f)
mp = append(mp, bak)
}
return &MultiLog{
mp: mp,
}
}
示例6: bind
func (a *Server) bind() (conn *net.UDPConn, err error) {
if !a.NotMulticast && a.iface.Flags&net.FlagMulticast == net.FlagMulticast {
gaddr, err := a.groupAddr()
if err != nil {
return nil, e.Forward(err)
}
conn, err = net.ListenMulticastUDP(a.Proto(), a.iface, gaddr)
if err != nil {
return nil, e.New(err)
}
} else {
server, err := net.ResolveUDPAddr(a.Proto(), ":"+a.Port)
if err != nil {
return nil, e.New(err)
}
conn, err = net.ListenUDP(a.Proto(), server)
if err != nil {
return nil, e.New(err)
}
}
a.ipver(conn.LocalAddr())
_, a.Port, err = utilNet.SplitHostPort(conn.LocalAddr().String())
if err != nil {
return nil, e.Forward(err)
}
return
}
示例7: Put
// Put inserts in the database the struct in v, v must have the fild _id and
// the field _rev both strings. Like the TestStruct struct.
func (c *Couch) Put(v interface{}) (id, rev string, err error) {
data, err := json.Marshal(v)
if err != nil {
return "", "", e.Push(err, "can't serialize the data")
}
id = grepId(data)
if id == "" {
return "", "", e.New("document have no id")
}
code, resp, err := c.put(id, "", data)
if err != nil && code == 409 {
return "", "", e.Push(err, ErrDocConflict)
} else if err != nil {
return "", "", e.Push(err, "can't put the document")
}
if code == http.StatusCreated {
ir := new(response)
err := json.Unmarshal(resp, ir)
if err != nil {
return "", "", e.Push(e.Push(err, "can't desserialize returned data"), "can't put the document")
}
if !ir.Ok {
return "", "", e.New("put failed")
}
return ir.Id, ir.Rev, nil
}
return "", "", e.Push(e.Push(err, e.New("received wrong response code, %v", code)), "can't put the document")
}
示例8: PingHttp
// PingHttp connect a http or https server and try to
// receive something. If the server return a code different
// of 2xx, it will fail. Ignores insecure certificates.
func PingHttp(url *url.URL) error {
resp, err := httpClient.Get(url.String())
if e.Contains(err, "connection refused") {
return e.Push(e.New(err), "get failed: connection refused")
} else if err != nil {
return e.Push(e.New(err), "get failed")
}
defer resp.Body.Close()
buf := make([]byte, 4096)
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
n, err := resp.Body.Read(buf)
if err != nil && err != io.EOF {
return e.Forward(err)
}
buf = buf[:n]
//status.Log(status.Protocol, "PingHttp status code is %v and received it from server: %v", resp.StatusCode, string(buf))
log.ProtoLevel().Printf("PingHttp status code is %v and received it from server: %v", resp.StatusCode, string(buf))
return e.New("returned status code %v, expected 2xx", resp.StatusCode)
}
_, err = resp.Body.Read(buf)
if err != nil && err != io.EOF {
return e.Forward(err)
}
return nil
}
示例9: TypeOf
// TypeOf returns the type of val. If samething wrong occur return a error.
// This functoin don't panic.
func TypeOf(val reflect.Value) (t reflect.Type, err error) {
defer func() {
r := recover()
if r != nil {
switch v := r.(type) {
case error:
err = e.Forward(v)
case string:
if strings.Contains(v, "runtime error") {
panic(r)
}
err = e.New(v)
default:
err = e.New("unable to type switch the recover data")
}
}
}()
t = val.Type()
if t.Kind() == reflect.Interface {
if val.Elem().IsValid() {
t = val.Elem().Type()
} else {
err = e.New("invalid interface")
}
}
return
}
示例10: Put
func (t *transaction) Put(key string, data interface{}) error {
if !t.write {
return e.New(ErrReadOnly)
}
if key == "" {
return e.New(ErrInvKey)
}
t.m[key] = data
idx := *t.idx
defer func() {
*t.idx = idx
}()
if len(idx) == 0 {
idx = append(idx, key)
return nil
}
i := searchString(idx, key)
if i >= len(idx) || idx[i] != key {
if i+1 > len(idx) {
idx = append(idx, key)
return nil
}
idx = append(idx[:i], append([]string{key}, idx[i:]...)...)
}
return nil
}
示例11: Delete
// Delete deletes from database the id id with rev revision.
// If rev is empty Delete deletes the last revision.
func (c *Couch) Delete(id, rev string) (string, error) {
if rev == "" {
revision := new(idRev)
err := c.Get(id, "", revision)
if e.Equal(err, ErrCantGetDoc) {
return "", e.Push(err, ErrDocDontExist)
} else if err != nil {
return "", e.Push(err, "not deleted")
}
rev = revision.Rev
}
rev = "rev=" + rev
code, resp, err := c.delete(id, rev, nil)
if err != nil && code == 404 {
return "", e.Push(err, ErrDocDontExist)
} else if err != nil {
return "", e.Push(err, "not deleted")
}
if code != http.StatusOK && code != http.StatusAccepted {
return "", e.New("can't delete the document, wrong code: %v", code)
}
dr := new(response)
err = json.Unmarshal(resp, dr)
if err != nil {
return "", e.Push(e.Push(err, "can't desserialize returned data"), "can't put the document")
}
if !dr.Ok {
return "", e.New("delete failed")
}
if dr.Id != id {
return "", e.New("response with the wrong id")
}
return dr.Rev, nil
}
示例12: do
func (c *client) do(method, path, params string, body []byte) (int, []byte, error) {
url := utilUrl.Copy(c.url)
url.Path += path
url.RawQuery = params
var buf io.Reader
if len(body) > 0 {
buf = bytes.NewBuffer(body)
}
req, err := http.NewRequest(method, url.String(), buf)
if err != nil {
return 0, nil, e.New("can't create request")
}
if c.url.User != nil {
req.Header.Set("Authorization", authorizationHeader(url.User.String()))
}
req.Header.Set("Content-Type", "application/json")
resp, err := HttpClient.Do(req)
if err != nil {
return 0, nil, e.Push(err, "can't put")
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return resp.StatusCode, nil, e.New("wrong status code - %v: %v", resp.StatusCode, resp.Status)
}
var data []byte
data, _ = ioutil.ReadAll(resp.Body)
return resp.StatusCode, data, nil
}
示例13: Construct
func (c *Constructors) Construct(name string, args ...interface{}) (reflect.Value, error) {
c.lck.Lock()
defer c.lck.Unlock()
con, found := c.constructors[name]
if !found {
t, err := types.GetType(name)
if err != nil {
return reflect.Value{}, e.New(ErrTypeNotFound)
}
return types.Make(t), nil
}
vals := make([]reflect.Value, 0, len(args))
for _, arg := range args {
vals = append(vals, reflect.ValueOf(arg))
}
retvals := con.Call(vals)
if len(retvals) != 2 {
return reflect.Value{}, e.New(ErrInvReturn)
}
if retvals[1].Interface() != nil {
err, ok := retvals[1].Interface().(error)
if !ok {
return reflect.Value{}, e.New(ErrInvReturn)
}
if err != nil {
return reflect.Value{}, e.Forward(err)
}
}
return retvals[0], nil
}
示例14: getInt
func (i *Intface) getInt() error {
if i.Interface != "" && i.iface == nil {
ints, err := net.Interfaces()
if err != nil {
return e.New(err)
}
for _, in := range ints {
if in.Name == i.Interface {
i.iface = &in
return nil
}
}
return e.New("none interface with this name")
} else if i.Interface == "" && i.iface == nil {
ints, err := net.Interfaces()
if err != nil {
return e.New(err)
}
var intName string
for _, in := range ints {
if in.Flags&net.FlagMulticast == net.FlagMulticast || in.Flags&net.FlagBroadcast == net.FlagBroadcast || in.Flags&net.FlagBroadcast == net.FlagBroadcast {
_, intName = getInterface(in)
if intName == "" {
continue
}
i.Interface = intName
i.iface = &in
break
}
}
}
return nil
}
示例15: NewStdFormatter
// NewStdFormatter crete a new formatter.
func NewStdFormatter(delim, tmpl string, entry Entry, values map[string]interface{}, timeformat string) (Formatter, error) {
if delim == "" {
return nil, e.New("invalid delimitator")
}
if tmpl == "" {
return nil, e.New("invalid template")
}
if entry == nil {
return nil, e.New("invalid entry")
}
if values == nil {
return nil, e.New("invalid values")
}
if timeformat == "" {
timeformat = TimeDateFormat
}
return &StdFormatter{
Delim: []byte(delim),
Tmpl: []byte(tmpl),
E: entry,
Map: values,
Idx: mkindex(entry),
TimeFormat: timeformat,
}, nil
}