本文整理汇总了Golang中errors.New函数的典型用法代码### 示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。
在下文中一共展示了New函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。
示例1: ParseTimeInterval
func ParseTimeInterval(source string) (time.Duration, error) {
matches := intervalRegex.FindStringSubmatch(strings.ToLower(source))
if matches == nil {
return 0, errors.New("Invalid time interval " + source)
}
val, err := strconv.Atoi(matches[1])
if err != nil {
return 0, err
}
switch matches[2] {
case "s":
return time.Duration(val) * time.Second, nil
case "m":
return time.Duration(val) * time.Second * 60, nil
case "h":
return time.Duration(val) * time.Second * 60 * 60, nil
case "d":
return time.Duration(val) * time.Second * 60 * 60 * 24, nil
case "w":
return time.Duration(val) * time.Second * 60 * 60 * 24 * 7, nil
default:
return 0, errors.New("Invalid time interval " + source)
}
}
开发者ID:nagyistge,项目名称:gotelemetry_agent,代码行数:33,代码来源:interval.go
示例2: Run
func (this *MemeOCR) Run(image string) (*OCRResult, error) {
imageTif := fmt.Sprintf("%s_meme.jpg", image)
outText := fmt.Sprintf("%s_meme", image)
preprocessingArgs := []string{image, "-resize", "400%", "-fill", "black", "-fuzz", "10%", "+opaque", "#FFFFFF", imageTif}
tesseractArgs := []string{"-l", "meme", imageTif, outText}
err := runProcessorCommand(GetExecPath(), preprocessingArgs)
if err != nil {
return nil, errors.New(fmt.Sprintf("Meme preprocessing command failed with error = %v", err))
}
defer os.Remove(imageTif)
err = runProcessorCommand("tesseract", tesseractArgs)
if err != nil {
return nil, errors.New(fmt.Sprintf("Meme tesseract command failed with error = %v", err))
}
defer os.Remove(outText + ".txt")
text, err := ioutil.ReadFile(outText + ".txt")
if err != nil {
return nil, err
}
result := strings.ToLower(strings.TrimSpace(string(text[:])))
return newOCRResult(this.name, result), nil
}
开发者ID:rizzles,项目名称:convert,代码行数:27,代码来源:ocrcommands.go
示例3: UnmarshalJSON
func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConnectionConfig struct {
Protocol string `json:"protocol"`
SendThrough *v2net.AddressJson `json:"sendThrough"`
StreamSetting *internet.StreamSettings `json:"streamSettings"`
Settings json.RawMessage `json:"settings"`
}
jsonConfig := new(JsonConnectionConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return errors.New("Point: Failed to parse outbound config: " + err.Error())
}
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
if jsonConfig.SendThrough != nil {
address := jsonConfig.SendThrough.Address
if address.Family().IsDomain() {
return errors.New("Point: Unable to send through: " + address.String())
}
this.SendThrough = address
}
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
return nil
}
开发者ID:DZLZHCODE,项目名称:v2ray-core,代码行数:26,代码来源:config_json.go
示例4: Save
// Save saves an mbox file from a mbox.Form!
func Save(form *Form) error {
t := time.Now()
if form.Email == "@" || form.Email == " " || !strings.ContainsAny(form.Email, "@") || !strings.ContainsAny(form.Email, ".") {
return errors.New("Blank email address.")
}
if ValidationLevel != 1 {
err := emailx.Validate(form.Email)
if err != nil {
if err == emailx.ErrInvalidFormat {
return errors.New("Email is not valid format.")
}
if err == emailx.ErrUnresolvableHost {
return errors.New("Email is not valid format.")
}
return errors.New("Email is not valid format." + err.Error())
}
}
// Normalize email address capitalization
form.Email = emailx.Normalize(form.Email)
// mbox files use two different date formats apparently.
mailtime := t.Format("Mon Jan 2 15:04:05 2006")
mailtime2 := t.Format("Mon, 2 Jan 2006 15:04:05 -0700")
Mail.Println("From " + form.Email + " " + mailtime)
Mail.Println("Return-path: <" + form.Email + ">")
Mail.Println("Envelope-to: " + Destination)
Mail.Println("Delivery-date: " + mailtime2)
Mail.Println("To: " + Destination)
Mail.Println("Subject: " + form.Subject)
Mail.Println("From: " + form.Email)
Mail.Println("Date: " + mailtime2)
Mail.Println("\n" + form.Message + "\n\n")
return nil
}
开发者ID:aerth,项目名称:cosgo,代码行数:34,代码来源:mbox.go
示例5: evalStarExpr
func evalStarExpr(ctx *Ctx, starExpr *StarExpr, env *Env) (*reflect.Value, bool, error) {
// return nil, false, errors.New("Star expressions not done yet")
var cexpr Expr
var errs []error
if cexpr, errs = CheckExpr(ctx, starExpr.X, env); len(errs) != 0 {
for _, cerr := range errs {
fmt.Printf("%v\n", cerr)
}
return nil, false, errors.New("Something wrong checking * expression")
}
xs, _, err := EvalExpr(ctx, cexpr, env)
if err != nil {
return nil, false, err
} else if xs == nil {
// XXX temporary error until typed evaluation of nil
return nil, false, errors.New("Cannot dereferece nil type")
}
var x reflect.Value
if x, err = expectSingleValue(ctx, *xs, starExpr.X); err != nil {
return nil, false, err
}
switch x.Type().Kind() {
case reflect.Interface, reflect.Ptr:
val := x.Elem()
return &val, true, nil
default:
return nil, true, ErrInvalidIndirect{x.Type()}
}
}
开发者ID:raff,项目名称:eval,代码行数:32,代码来源:starexpr.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng