本文整理匯總了Golang中github.com/dropbox/godropbox/errors.Newf函數的典型用法代碼示例。如果您正苦於以下問題:Golang Newf函數的具體用法?Golang Newf怎麽用?Golang Newf使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Newf函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SampleInts
// Samples 'k' unique ints from the range [0, n)
func SampleInts(n int, k int) (res []int, err error) {
if k < 0 {
err = errors.Newf("invalid sample size k")
return
}
if n < k {
err = errors.Newf("sample size k larger than n")
return
}
picked := set.NewSet()
for picked.Len() < k {
i := Intn(n)
picked.Add(i)
}
res = make([]int, k)
e := 0
for i := range picked.Iter() {
res[e] = i.(int)
e++
}
return
}
示例2: serializeClauses
func serializeClauses(
clauses []Clause,
separator []byte,
out *bytes.Buffer) (err error) {
if clauses == nil || len(clauses) == 0 {
return errors.Newf("Empty clauses. Generated sql: %s", out.String())
}
if clauses[0] == nil {
return errors.Newf("nil clause. Generated sql: %s", out.String())
}
if err = clauses[0].SerializeSql(out); err != nil {
return
}
for _, c := range clauses[1:] {
out.Write(separator)
if c == nil {
return errors.Newf("nil clause. Generated sql: %s", out.String())
}
if err = c.SerializeSql(out); err != nil {
return
}
}
return nil
}
示例3: SerializeSqlForColumnList
func (c *aliasColumn) SerializeSqlForColumnList(includeTableName bool, database Database, out *bytes.Buffer) error {
if !validIdentifierName(c.name) {
return errors.Newf(
"Invalid alias name `%s`. Generated sql: %s",
c.name,
out.String())
}
if c.expression == nil {
return errors.Newf(
"Cannot alias a nil expression. Generated sql: %s",
out.String())
}
out.WriteByte('(')
if c.expression == nil {
return errors.Newf("nil alias clause. Generate sql: %s", out.String())
}
if err := c.expression.SerializeSql(database, out); err != nil {
return err
}
out.WriteString(") AS ")
out.WriteRune(database.EscapeCharacter())
out.WriteString(c.name)
out.WriteRune(database.EscapeCharacter())
return nil
}
示例4: SerializeSqlForColumnList
func (c *aliasColumn) SerializeSqlForColumnList(out *bytes.Buffer) error {
if !validIdentifierName(c.name) {
return errors.Newf(
"Invalid alias name `%s`. Generated sql: %s",
c.name,
out.String())
}
if c.expression == nil {
return errors.Newf(
"Cannot alias a nil expression. Generated sql: %s",
out.String())
}
out.WriteByte('(')
if c.expression == nil {
return errors.Newf("nil alias clause. Generate sql: %s", out.String())
}
if err := c.expression.SerializeSql(out); err != nil {
return err
}
out.WriteString(") AS `")
out.WriteString(c.name)
out.WriteByte('`')
return nil
}
示例5: parseRowsHeader
func (p *baseRowsEventParser) parseRowsHeader(raw *RawV4Event) (
id uint64,
flags uint16,
extraInfo []byte,
width int,
remaining []byte,
err error) {
if p.context == nil {
return 0, 0, nil, 0, nil, &TableContextNotSetError{
errors.New("Table context not set"),
}
}
data := raw.FixedLengthData()
id = LittleEndian.Uint48(data)
if id != p.context.TableId() {
return 0, 0, nil, 0, nil, errors.Newf(
"mismatch table id (event: %d; context: %d name: %s)",
id,
p.context.TableId(),
p.context.TableName())
}
flags = LittleEndian.Uint16(data[6:])
remaining = raw.VariableLengthData()
if p.version == mysql_proto.RowsEventVersion_V1 {
extraInfo = nil
} else {
extraInfoBlobLen := LittleEndian.Uint16(data[8:])
extraInfo, remaining, err = p.parseExtraInfoBlob(
extraInfoBlobLen,
remaining)
if err != nil {
return 0, 0, nil, 0, nil, err
}
}
w, remaining, err := readFieldLength(remaining)
if err != nil {
return 0, 0, nil, 0, nil, err
}
if w > uint64(p.context.NumColumns()) {
return 0, 0, nil, 0, nil, errors.Newf(
"row width (%d / %d) greater than # of columns (%d) in table %s",
w,
width,
p.context.NumColumns(),
string(p.context.TableName()))
}
return id, flags, extraInfo, int(w), remaining, nil
}
示例6: validate
func (s *Source) validate() (err error) {
if strings.ToLower(s.Hash) == "skip" {
return
}
file, err := os.Open(s.Path)
if err != nil {
err = &HashError{
errors.Wrap(err, "source: Failed to open file for hash"),
}
return
}
defer file.Close()
var hash hash.Hash
switch len(s.Hash) {
case 32:
hash = md5.New()
case 40:
hash = sha1.New()
case 64:
hash = sha256.New()
case 128:
hash = sha512.New()
default:
err = &HashError{
errors.Newf("source: Unknown hash type for hash '%s'", s.Hash),
}
return
}
_, err = io.Copy(hash, file)
if err != nil {
return
}
sum := hash.Sum([]byte{})
hexSum := fmt.Sprintf("%x", sum)
if hexSum != s.Hash {
err = &HashError{
errors.Newf("source: Hash verification failed for '%s'", s.Source),
}
return
}
return
}
示例7: Register
// See ResourcePool for documentation.
func (p *MultiResourcePool) Register(resourceLocation string) error {
if resourceLocation == "" {
return errors.New("Registering invalid resource location")
}
p.rwMutex.Lock()
defer p.rwMutex.Unlock()
if p.isLameDuck {
return errors.Newf(
"Cannot register %s to lame duck resource pool",
resourceLocation)
}
if _, inMap := p.locationPools[resourceLocation]; inMap {
return nil
}
pool := p.createPool(p.options)
if err := pool.Register(resourceLocation); err != nil {
return err
}
p.locationPools[resourceLocation] = pool
return nil
}
示例8: CreateImage
// Writes an image to a file. The extension of the file is used to determine the
// encoding method.
func CreateImage(i image.Image, file string) error {
ext := filepath.Ext(file)
m := mime.TypeByExtension(ext)
switch m {
case "image/jpeg", "image/png", "image/tiff":
default:
return errors.Newf("unsupported extension/mime type: %s %s", ext, m)
}
f, err := os.Create(file)
if err != nil {
return errors.Wrap(err, "could not create file")
}
defer f.Close()
switch m {
case "image/jpeg":
err = jpeg.Encode(f, i, &jpeg.Options{Quality: 98})
case "image/png":
err = png.Encode(f, i)
case "image/tiff":
err = tiff.Encode(f, i, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
default:
panic("unreachable")
}
if err != nil {
return errors.Wrap(err, "could not encode image")
}
return nil
}
示例9: Peek
// Peek returns a slice of the look ahead buffer which holds numBytes
// number of bytes. If the look ahead buffer does not already hold enough
// bytes, it will read from the underlying reader to populate the rest.
// NOTE: the returned slice is not a copy of the raw buffer.
func (b *LookAheadBuffer) Peek(numBytes int) ([]byte, error) {
if numBytes < 0 {
return nil, errors.New("Cannot fill negative numBytes")
}
if numBytes > len(b.buffer) {
return nil, errors.Newf(
"Buffer full (buffer size: %d n: %d)",
len(b.buffer),
numBytes)
}
var err error
var numRead int
if b.bytesBuffered < numBytes {
numRead, err = io.ReadAtLeast(
b.src,
b.buffer[b.bytesBuffered:],
numBytes-b.bytesBuffered)
if err != nil {
if err == io.ErrUnexpectedEOF {
// ErrUnexpectedEOF is a terrible error only returned by
// ReadAtLeast. Return EOF (i.e., the original error) instead
// ErrUnexpectedEOF since no one ever checks for this.
err = io.EOF
}
}
}
b.bytesBuffered += numRead
if numBytes > b.bytesBuffered {
numBytes = b.bytesBuffered
}
return b.buffer[:numBytes], err
}
示例10: Register
// See ConnectionPool for documentation.
func (p *MultiConnectionPool) Register(network string, address string) error {
if network == "" && address == "" {
return errors.New("Registering invalid (network, address)")
}
key := NetworkAddress{
Network: network,
Address: address,
}
p.rwMutex.Lock()
defer p.rwMutex.Unlock()
if p.isLameDuck {
return errors.Newf(
"Cannot register (%s, %s) to lame duck connection pool",
network,
address)
}
if _, inMap := p.addressPools[key]; inMap {
return nil
}
pool := p.createPool(p.options)
if err := pool.Register(network, address); err != nil {
return err
}
p.addressPools[key] = pool
return nil
}
示例11: GetPacker
func GetPacker(pac *pack.Pack, distro, release string) (
pcker Packer, err error) {
switch constants.DistroPack[distro] {
case "pacman":
pcker = &pacman.Pacman{
Pack: pac,
}
case "debian":
pcker = &debian.Debian{
Pack: pac,
}
case "redhat":
pcker = &redhat.Redhat{
Pack: pac,
}
default:
system := distro
if release != "" {
system += "-" + release
}
err = &UnknownSystem{
errors.Newf("packer: Unkown system %s", system),
}
return
}
return
}
示例12: SerializeSql
func (conj *conjunctExpression) SerializeSql(out *bytes.Buffer) (err error) {
if len(conj.expressions) == 0 {
return errors.Newf(
"Empty conjunction. Generated sql: %s",
out.String())
}
clauses := make([]Clause, len(conj.expressions), len(conj.expressions))
for i, expr := range conj.expressions {
clauses[i] = expr
}
use_parentheses := len(clauses) > 1
if use_parentheses {
out.WriteByte('(')
}
if err = serializeClauses(clauses, conj.conjunction, out); err != nil {
return
}
if use_parentheses {
out.WriteByte(')')
}
return nil
}
示例13: SerializeSql
func (arith *arithmeticExpression) SerializeSql(out *bytes.Buffer) (err error) {
if len(arith.expressions) == 0 {
return errors.Newf(
"Empty arithmetic expression. Generated sql: %s",
out.String())
}
clauses := make([]Clause, len(arith.expressions), len(arith.expressions))
for i, expr := range arith.expressions {
clauses[i] = expr
}
useParentheses := len(clauses) > 1
if useParentheses {
out.WriteByte('(')
}
if err = serializeClauses(clauses, arith.operator, out); err != nil {
return
}
if useParentheses {
out.WriteByte(')')
}
return nil
}
示例14: TestGet
func (*singletonSuite) TestGet(c *C) {
callCount := 0
init := func() (interface{}, error) {
callCount++
if callCount < 2 {
return nil, errors.Newf("fail")
}
v := 5
return &v, nil
}
s := NewSingleton(init)
// Verify init hasn't been called yet
c.Assert(callCount, Equals, 0)
// Verify init gets called with Get()
_, err := s.Get()
c.Assert(err, NotNil)
c.Assert(callCount, Equals, 1)
// Verify failed init gets retried with Get()
data1, err := s.Get()
c.Assert(err, IsNil)
c.Assert(callCount, Equals, 2)
c.Assert(*(data1.(*int)), Equals, 5)
// Verify that subsequent Get() after a successful one return the same object and don't call
// init
data2, err := s.Get()
c.Assert(err, IsNil)
c.Assert(callCount, Equals, 2)
c.Assert(data1, Equals, data2)
}
示例15: Parse
func Parse() (err error) {
flag.Parse()
cmd := flag.Arg(0)
switch cmd {
case "build":
err = Build()
case "create":
err = Create()
case "project":
err = Project()
case "pull":
err = utils.PullContainers()
case "genkey":
err = GenKey()
case "list-targets":
err = ListTargets()
default:
err = &UnknownCommand{
errors.Newf("cmd: Unknown command '%s'", cmd),
}
}
return
}