當前位置: 首頁>>代碼示例>>Golang>>正文


Golang errors.New函數代碼示例

本文整理匯總了Golang中github.com/Monnoroch/golfstream/errors.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getBaseConfig

func getBaseConfig(cfg interface{}) (interface{}, error) {
	c, ok := cfg.(map[string]interface{})
	if !ok {
		return nil, errors.New(fmt.Sprintf("getBaseConfig: Expected map[string]interface{}, got %v", cfg))
	}

	if _, ok := c["remote"]; !ok {
		return cfg, nil
	}

	a, ok := c["arg"]
	if !ok {
		return nil, errors.New(fmt.Sprintf("getBaseConfig: Expected config to have field \"arg\", got %v", cfg))
	}

	aa, ok := a.(map[string]interface{})
	if !ok {
		return nil, errors.New(fmt.Sprintf("getBaseConfig: Expected config.arg to have field \"arg\" of map[string]interface{}, got %v", a))
	}

	base, ok := aa["base"]
	if !ok {
		return nil, errors.New(fmt.Sprintf("getBaseConfig: Expected config.arg to have field \"base\", got %v", a))
	}

	return base, nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:27,代碼來源:remote.go

示例2: convRange

func convRange(from int, to int, l int, fn string) (uint, uint, error) {
	if from < 0 {
		from = l + 1 + from
	}
	if from < 0 {
		from = 0
	}
	if to < 0 {
		to = l + 1 + to
	}
	if to < 0 {
		return 0, 0, errors.New(fmt.Sprintf("%s: to:%v < 0", fn, to))
	}
	if from > l {
		return 0, 0, errors.New(fmt.Sprintf("%s: from:%v > len:%v", fn, from, l))
	}
	if to > l {
		return 0, 0, errors.New(fmt.Sprintf("%s: to:%v > len:%v", fn, to, l))
	}
	if from > to {
		return 0, 0, errors.New(fmt.Sprintf("%s: from:%v > to:%v", fn, from, to))
	}

	return uint(from), uint(to), nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:25,代碼來源:nil.go

示例3: AddStream

func (self *serviceBackend) AddStream(bstream, name string, defs []string) (res backend.BackendStream, rerr error) {
	self.lock.Lock()
	defer self.lock.Unlock()

	if _, ok := self.streams[name]; ok {
		return nil, errors.New(fmt.Sprintf("serviceBackend.AddStream: backend with name \"%s\" already has stream \"%s\"", self.name, name))
	}

	bs, ok := self.bstreams[bstream]
	if !ok {
		bstr, err := self.back.GetStream(bstream)
		if err != nil {
			return nil, err
		}

		bs = &backendStreamT{bstr, self.name, bstream, self.async, sync.Mutex{}, []backend.Stream{bstr}, 0}
		self.bstreams[bstream] = bs
	}

	st := &valueStream{nil, false, false, false}
	data, err := stream.Run(st, defs)
	if err != nil {
		return nil, err
	}

	s := &streamT{bs, defs, st, data}
	self.streams[name] = s
	bs.refcnt += 1
	return s, nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:30,代碼來源:service.go

示例4: addSub

func (self *remoteService) addSub(back, bname string, sid uint32, hFrom int, hTo int) (uint, uint, error) {
	cmd := addSubCmd{
		Cmd: "subscribe",
		Data: addSubCmdData{
			Id:    self.getCmdId(),
			Back:  back,
			Bname: bname,
			Sid:   sid,
			From:  hFrom,
			To:    hTo,
		},
	}

	v, err := self.getCmdRes(cmd.Data.Id, &cmd)
	if err != nil {
		return 0, 0, err
	}

	rr := rangeRes{}
	if err := json.NewDecoder(bytes.NewReader(v)).Decode(&rr); err != nil {
		return 0, 0, err
	}

	if rr.Err != "" {
		return 0, 0, errors.New(rr.Err)
	}

	return rr.From, rr.To, nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:29,代碼來源:remote.go

示例5: Config

func (self *httpBackend) Config() (interface{}, error) {
	resp, err := self.p.Post(self.configUrl, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	res := configRes{}
	if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
		return nil, err
	}

	if res.Err != "" {
		return nil, errors.New(res.Err)
	}

	return map[string]interface{}{
		"type":   "http",
		"remote": true,
		"arg": map[string]interface{}{
			"url":  self.baseUrl,
			"base": res.Cfg,
		},
	}, nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:25,代碼來源:http.go

示例6: Next

func (self *minByStream) Next() (Event, error) {
	if self.done {
		return nil, EOI
	}

	for {
		data, err1 := self.datas.Next()
		v, err2 := self.vals.Next()

		if err := getError(err1, err2); err != nil {
			if err == EOI {
				self.done = true
				return self.data, nil
			}
			return nil, err
		}

		val, ok := getIntOrFloat(v)
		if !ok {
			return nil, errors.New(fmt.Sprintf("MinBy: Expected number event, got %v", v))
		}

		if val < self.val {
			self.data = data
			self.val = val
		}
	}
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:28,代碼來源:streams.go

示例7: Read

func (self *httpBackendStream) Read(from uint, to uint) (stream.Stream, error) {
	if from == to {
		return stream.Empty(), nil
	}

	resp, err := self.p.Post(fmt.Sprintf(self.readUrl, from, to), nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	res := arrErrorObj{}
	if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
		return nil, err
	}

	if res.Err != "" {
		return nil, errors.New(res.Err)
	}

	r := make([]stream.Event, len(res.Events))
	for i, v := range res.Events {
		r[i] = stream.Event([]byte(v))
	}
	return stream.List(r), nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:26,代碼來源:http.go

示例8: rmSub

func (self *remoteService) rmSub(back, bname string, sid uint32) (bool, error) {
	cmd := rmSubCmd{
		Cmd: "unsubscribe",
		Data: rmSubCmdData{
			Id:    self.getCmdId(),
			Back:  back,
			Bname: bname,
			Sid:   sid,
		},
	}

	v, err := self.getCmdRes(cmd.Data.Id, &cmd)
	if err != nil {
		return false, err
	}

	rr := okRes{}
	if err := json.NewDecoder(bytes.NewReader(v)).Decode(&rr); err != nil {
		return false, err
	}

	if rr.Err != "" {
		return false, errors.New(rr.Err)
	}

	return rr.Ok, nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:27,代碼來源:remote.go

示例9: pushToSub

func (self *remoteServiceBackend) pushToSub(sid uint32, evt stream.Event) error {
	s, ok := self.getSub(sid)
	if !ok {
		return errors.New(fmt.Sprintf("remoteServiceBackend.pushToSub: Backend \"%s\" doesn't have subscriber %v", sid))
	}

	return s.Add(evt)
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:8,代碼來源:remote.go

示例10: GetField

/*
Creates a stream with events which are values of a field in events of the original stream as in JSON.
Returns an error if the original field doesn't exist.

The field might be deep inside, as in "object.value.data".
*/
func GetField(stream Stream, field string) Stream {
	return Map(stream, func(evt Event) (Event, error) {
		res, ok := getFieldImpl(evt, field)
		if !ok {
			return nil, errors.New(fmt.Sprintf("GetField: Expected event to have field %s, got %v", field, evt))
		}
		return res, nil
	})
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:15,代碼來源:streams.go

示例11: LessEqVal

/*
Creates a boolean stream with true events when the event of an original stream is less or equal to a given value and false events otherwise.

Original stream must consist of numbers.
*/
func LessEqVal(stream Stream, val float64) Stream {
	return Map(stream, func(e Event) (Event, error) {
		v, ok := getIntOrFloat(e)
		if !ok {
			return nil, errors.New(fmt.Sprintf("LessEqVal: Expected event to be number, got %v", e))
		}

		return v <= val, nil
	})
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:15,代碼來源:streams.go

示例12: StringPrepend

/*
Takes a stream af strings and prepend a given string to all of them.
*/
func StringPrepend(stream Stream, pref string) Stream {
	return Map(stream, func(val Event) (Event, error) {
		v, ok := val.(string)
		if !ok {
			return nil, errors.New(fmt.Sprintf("StringPrepend: Expected event to be string, got %v", v))
		}

		return pref + v, nil
	})
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:13,代碼來源:streams.go

示例13: handleEvent

func (self *remoteService) handleEvent(back string, sid uint32, evt stream.Event) error {
	self.lock.Lock()
	b, ok := self.backends[back]
	self.lock.Unlock()
	if !ok {
		return errors.New(fmt.Sprintf("remoteService.handleEvent: No backend with name \"%v\"", back))
	}

	return b.pushToSub(sid, evt)
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:10,代碼來源:remote.go

示例14: handleCmdRes

func (self *remoteService) handleCmdRes(id uint32, data json.RawMessage) error {
	ch := self.popCmd(id)
	if ch == nil {
		return errors.New(fmt.Sprintf("remoteService.handleCmdRes: No command with id %v!", id))
	}

	ch <- data
	close(ch)
	return nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:10,代碼來源:remote.go

示例15: GetBackend

func (self *service) GetBackend(back string) (Backend, error) {
	self.lock.Lock()
	defer self.lock.Unlock()

	v, ok := self.backends[back]
	if !ok {
		return nil, errors.New(fmt.Sprintf("service.GetBackend: backend with name \"%s\" does not exist", back))
	}

	return v, nil
}
開發者ID:Monnoroch,項目名稱:golfstream,代碼行數:11,代碼來源:service.go


注:本文中的github.com/Monnoroch/golfstream/errors.New函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。