本文整理匯總了Golang中github.com/mewkiz/pkg/errutil.Err函數的典型用法代碼示例。如果您正苦於以下問題:Golang Err函數的具體用法?Golang Err怎麽用?Golang Err使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Err函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: initGameSession
// initGameSession if a load file exists load the old game otherwise
// create a new game session.
func initGameSession(a *area.Area) (sav *save.Save, err error) {
path, err := goutil.SrcDir("github.com/karlek/reason/")
if err != nil {
return nil, errutil.Err(err)
}
sav, err = save.New(path + "debug.save")
if err != nil {
return nil, errutil.Err(err)
}
err = initGameLibs()
if err != nil {
return nil, errutil.Err(err)
}
// If save exists load old game session.
// Otherwise create a new game session.
if sav.Exists() {
err = load(sav, a)
} else {
err = newGame(a)
}
if err != nil {
return nil, errutil.Err(err)
}
// Initalize turn priority queue.
turn.Init(a)
return sav, nil
}
示例2: dump
// dump stores the graph as a DOT file and an image representation of the graph
// as a PNG file with filenames based on "-o" flag.
func dump(graph *dot.Graph) error {
// Store graph to DOT file.
dotPath := flagOut
if !flagQuiet {
log.Printf("Creating: %q\n", dotPath)
}
err := ioutil.WriteFile(dotPath, []byte(graph.String()), 0644)
if err != nil {
return errutil.Err(err)
}
// Generate an image representation of the graph.
if flagImage {
pngPath := pathutil.TrimExt(dotPath) + ".png"
if !flagQuiet {
log.Printf("Creating: %q\n", pngPath)
}
cmd := exec.Command("dot", "-Tpng", "-o", pngPath, dotPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return errutil.Err(err)
}
}
return nil
}
示例3: parseSubs
// parseSubs parses the graph representations of the given high-level control
// flow primitives. If unable to locate a subgraph, a second attempt is made by
// prepending $GOPATH/src/decomp.org/decomp/cmd/restructure/primitives/ to the
// subgraph path.
func parseSubs(subPaths []string) (subs []*graphs.SubGraph, err error) {
// Prepend $GOPATH/src/decomp.org/decomp/cmd/restructure/primitives/ to the path
// of missing subgraphs.
subDir, err := goutil.SrcDir("decomp.org/decomp/cmd/restructure/primitives")
if err != nil {
return nil, errutil.Err(err)
}
for i, subPath := range subPaths {
if ok, _ := osutil.Exists(subPath); !ok {
subPath = filepath.Join(subDir, subPath)
subPaths[i] = subPath
}
}
// Parse subgraphs representing control flow primitives.
for _, subPath := range subPaths {
sub, err := graphs.ParseSubGraph(subPath)
if err != nil {
return nil, errutil.Err(err)
}
subs = append(subs, sub)
}
return subs, nil
}
示例4: takeInput
// Wait for input and send output to client.
func takeInput(conn net.Conn) (err error) {
for {
query, err := bufioutil.NewReader(conn).ReadLine()
if err != nil {
if err.Error() == "EOF" {
break
}
return errutil.Err(err)
}
// Do something with the query
switch query {
case settings.QueryUpdates:
// Encode (send) the value.
err = gob.NewEncoder(conn).Encode(settings.Updates)
case settings.QueryClearAll:
settings.Updates = make(map[string]bool)
err = settings.SaveUpdates()
case settings.QueryForceRecheck:
pages, err := ini.ReadPages(settings.PagesPath)
if err != nil {
return errutil.Err(err)
}
err = page.ForceUpdate(pages)
}
if err != nil {
return errutil.Err(err)
}
}
return nil
}
示例5: watchConfig
func watchConfig(watcher *fsnotify.Watcher) (err error) {
for {
select {
case ev := <-watcher.Event:
if ev != nil {
if ev.Name == settings.ConfigPath {
// Read settings from config file.
err = ini.ReadSettings(settings.ConfigPath)
if err != nil {
return errutil.Err(err)
}
}
// Retrieve an array of pages from INI file.
pages, err := ini.ReadPages(settings.PagesPath)
if err != nil {
return errutil.Err(err)
}
err = page.ForceUpdate(pages)
if err != nil {
return errutil.Err(err)
}
}
case err = <-watcher.Error:
if err != nil {
return errutil.Err(err)
}
}
}
}
示例6: clean
// clean removes old cache files from cache root.
func clean() (err error) {
// Get a list of all pages.
pages, err := ini.ReadPages(settings.PagesPath)
if err != nil {
return errutil.Err(err)
}
// Get a list of all cached pages.
caches, err := ioutil.ReadDir(settings.CacheRoot)
if err != nil {
return errutil.Err(err)
}
for _, cache := range caches {
remove := true
for _, p := range pages {
pageName, err := filename.Encode(p.UrlAsFilename())
if err != nil {
return errutil.Err(err)
}
if cache.Name() == pageName+".htm" {
remove = false
break
}
}
if remove {
err = os.Remove(settings.CacheRoot + cache.Name())
if err != nil {
return err
}
}
}
return nil
}
示例7: ascii
// ascii opens an image file and prints an ascii art image.
func ascii(filename string) (err error) {
reader, err := os.Open(filename)
if err != nil {
return errutil.Err(err)
}
defer reader.Close()
i, _, err := image.Decode(reader)
if err != nil {
return errutil.Err(err)
}
// Image width and height.
width, height := i.Bounds().Dx(), i.Bounds().Dy()
// Different change in x and y values depending on the aspect ratio.
dx, dy := aspectRatio(width, height)
// Print each line.
var line string
for y := 0; y < height; y += dy {
line = ""
// Create a line. Convert the color level of the pixel into a ascii
// character.
for x := 0; x < width; x += dx {
line += level(i.At(x, y).RGBA())
}
fmt.Println(line)
}
return nil
}
示例8: ReadSettings
// ReadSettings reads settings file and updates settings.Global.
func ReadSettings(configPath string) (err error) {
// Parse config file.
file := ini.New()
err = file.Load(configPath)
if err != nil {
return errutil.Err(err)
}
config, settingExist := file.Sections[sectionSettings]
mail, mailExist := file.Sections[sectionMail]
if settingExist {
err = parseSettings(config)
if err != nil {
return errutil.Err(err)
}
}
if mailExist {
err = parseMail(mail)
if err != nil {
return errutil.Err(err)
}
}
return nil
}
示例9: readAll
// Opens all links with browser.
func readAll(bw *bufioutil.Writer, conn net.Conn) (err error) {
// Read in config file to settings.Global
err = ini.ReadSettings(settings.ConfigPath)
if err != nil {
return errutil.Err(err)
}
ups, err := getUpdates(bw, conn)
if err != nil {
return errutil.Err(err)
}
if settings.Global.Browser == "" {
fmt.Println("No browser path set in:", settings.ConfigPath)
return nil
}
// If no updates was found, ask for forgiveness.
if len(ups) == 0 {
fmt.Println("Sorry, no updates :(")
return nil
}
var arguments []string
// Loop through all updates and open them with the browser
for up := range ups {
arguments = append(arguments, up)
}
cmd := exec.Command(settings.Global.Browser, arguments...)
err = cmd.Start()
if err != nil {
return errutil.Err(err)
}
return nil
}
示例10: initGameLibs
// initGameLibs initializes creature, fauna and item libraries.
func initGameLibs() (err error) {
// Init graphic library.
err = termbox.Init()
if err != nil {
return errutil.Err(err)
}
// Initialize creatures.
err = creature.Load()
if err != nil {
return errutil.Err(err)
}
// Initialize fauna.
err = terrain.Load()
if err != nil {
return errutil.Err(err)
}
// Initialize items.
err = item.Load()
if err != nil {
return errutil.Err(err)
}
// Initialize Objects.
err = object.Load()
if err != nil {
return errutil.Err(err)
}
ui.SetTerminal()
return nil
}
示例11: NewFunc
// NewFunc returns a new function based on the given result type, function name,
// and function parameters.
func NewFunc(result, gname, params interface{}) (*ir.Function, error) {
if result, ok := result.(types.Type); ok {
name, err := getGlobalName(gname)
if err != nil {
return nil, errutil.Err(err)
}
var sig *types.Func
switch params := params.(type) {
case *Params:
sig, err = types.NewFunc(result, params.params, params.variadic)
if err != nil {
return nil, errutil.Err(err)
}
case nil:
sig, err = types.NewFunc(result, nil, false)
if err != nil {
return nil, errutil.Err(err)
}
default:
return nil, errutil.Newf("invalid function parameters specifier type; expected *Params, got %T", params)
}
return ir.NewFunction(name, sig), nil
}
return nil, errutil.Newf("invalid function result type; expected types.Type, got %T", result)
}
示例12: walls
// walls downloads wallpapers based on the given search query, output dir, and
// search options. Download at most n wallpapers (0 = infinite).
func walls(query string, dir string, n int, options []wallhaven.Option) error {
// Create output directory.
if err := os.MkdirAll(dir, 0755); err != nil {
return errutil.Err(err)
}
// Download at most n wallpapers (0 = infinite).
total := 0
for {
ids, err := wallhaven.Search(query, options...)
if err != nil {
return errutil.Err(err)
}
if len(ids) == 0 {
return nil
}
for _, id := range ids {
path, err := id.Download(dir)
if err != nil {
return errutil.Err(err)
}
fmt.Println(path)
// Download at most n wallpapers.
if total+1 == n {
return nil
}
total++
}
// Turn to the next result page.
options = nextPage(options)
}
}
示例13: Check
// Check performs a static semantic analysis check on the given file.
func Check(file *ast.File) (*Info, error) {
// Semantic analysis is done in two passes to allow for forward references.
// Firstly, the global declarations are added to the file-scope. Secondly,
// the global function declaration bodies are traversed to resolve
// identifiers and deduce the types of expressions.
// Identifier resolution.
info := &Info{
Types: make(map[ast.Expr]types.Type),
Scopes: make(map[ast.Node]*Scope),
}
if err := resolve(file, info.Scopes); err != nil {
return nil, errutil.Err(err)
}
// Type-checking.
if err := typecheck.Check(file, info.Types); err != nil {
return nil, errutil.Err(err)
}
// Semantic analysis.
if err := semcheck.Check(file); err != nil {
return nil, errutil.Err(err)
}
return info, nil
}
示例14: NewCharArray
// NewCharArray returns a character array constant based on the given array type
// and string.
func NewCharArray(typ types.Type, s string) (*Array, error) {
// Verify array type.
v := new(Array)
var ok bool
v.typ, ok = typ.(*types.Array)
if !ok {
return nil, fmt.Errorf("invalid type %q for array constant", typ)
}
var err error
s, err = unquote(s)
if err != nil {
return nil, errutil.Err(err)
}
// Verify array element types.
if len(s) != v.typ.Len() {
return nil, fmt.Errorf("incorrect number of elements in character array constant; expected %d, got %d", v.typ.Len(), len(s))
}
elemType := v.typ.Elem()
if !types.Equal(elemType, types.I8) {
return nil, fmt.Errorf("invalid character array element type; expected %q, got %q", types.I8, elemType)
}
for i := 0; i < len(s); i++ {
elem, err := NewInt(elemType, strconv.Itoa(int(s[i])))
if err != nil {
return nil, errutil.Err(err)
}
v.elems = append(v.elems, elem)
}
return v, nil
}
示例15: parseBasicBlock
// parseBasicBlock converts the provided LLVM IR basic block into a basic block
// in which the instructions have been translated to Go AST statement nodes but
// the terminator instruction is an unmodified LLVM IR value.
func parseBasicBlock(llBB llvm.BasicBlock) (bb *basicBlock, err error) {
name, err := getBBName(llBB.AsValue())
if err != nil {
return nil, err
}
bb = &basicBlock{name: name, phis: make(map[string][]*definition)}
for inst := llBB.FirstInstruction(); !inst.IsNil(); inst = llvm.NextInstruction(inst) {
// Handle terminator instruction.
if inst == llBB.LastInstruction() {
err = bb.addTerm(inst)
if err != nil {
return nil, errutil.Err(err)
}
return bb, nil
}
// Handle PHI instructions.
if inst.InstructionOpcode() == llvm.PHI {
ident, def, err := parsePHIInst(inst)
if err != nil {
return nil, errutil.Err(err)
}
bb.phis[ident] = def
continue
}
// Handle non-terminator instructions.
stmt, err := parseInst(inst)
if err != nil {
return nil, err
}
bb.stmts = append(bb.stmts, stmt)
}
return nil, errutil.Newf("invalid basic block %q; contains no instructions", name)
}