2017-06-15 14:57:20 +00:00
|
|
|
// Copyright 2017 The go-python Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package pyerrors holds functions returning an error.
|
|
|
|
package pyerrors
|
|
|
|
|
2021-04-04 19:55:27 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
2017-06-15 14:57:20 +00:00
|
|
|
|
|
|
|
// Div is a function for detecting errors.
|
|
|
|
func Div(i, j int) (int, error) {
|
|
|
|
if j == 0 {
|
|
|
|
return 0, errors.New("Divide by zero.")
|
|
|
|
}
|
|
|
|
return i / j, nil
|
|
|
|
}
|
2021-04-04 19:55:27 +00:00
|
|
|
|
|
|
|
type Stringer fmt.Stringer
|
|
|
|
type MyString string
|
|
|
|
|
|
|
|
func (t MyString) String() string { return string(t) }
|
|
|
|
|
|
|
|
// NewMyString converts a string to a custom MyString type.
|
|
|
|
// It is an error to pass an empty string value.
|
|
|
|
func NewMyString(val string) (stringer Stringer, err error) {
|
|
|
|
if val == "" {
|
|
|
|
err = errors.New("Empty string value.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return MyString(val), nil
|
|
|
|
}
|