当前位置: 首页>>代码示例>>Golang>>正文


Golang os.Open函数代码示例

本文整理汇总了Golang中os.Open函数的典型用法代码示例。如果您正苦于以下问题:Golang Open函数的具体用法?Golang Open怎么用?Golang Open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: main

func main() {
	filecsv := `C:\Users\yejianfeng\Desktop\mxm\skill.csv`
	file, err := os.Open(filecsv)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	reader := csv.NewReader(file)
	roleFolder := `C:\Users\yejianfeng\Desktop\mxm\skill\`
	reader.Read()
	for {
		fields, err := reader.Read()
		if err == io.EOF {
			break
		}

		picurl := fields[15]

		jpg := roleFolder + picurl + ".jpg"
		_, err = os.Open(jpg)
		if err == nil || os.IsExist(err) {
			continue
		}

		png := roleFolder + picurl + ".png"
		_, err = os.Open(png)
		if err == nil || os.IsExist(err) {
			continue
		}

		fmt.Println(fields[0])
	}
}
开发者ID:atree1987,项目名称:MyWorks,代码行数:34,代码来源:checkskill.go

示例2: ReadDir

// ReadDir returns a Dir representing an expanded charm directory.
func ReadDir(path string) (dir *Dir, err error) {
	dir = &Dir{Path: path}
	file, err := os.Open(dir.join("metadata.yaml"))
	if err != nil {
		return nil, err
	}
	dir.meta, err = ReadMeta(file)
	file.Close()
	if err != nil {
		return nil, err
	}
	file, err = os.Open(dir.join("config.yaml"))
	if _, ok := err.(*os.PathError); ok {
		dir.config = NewConfig()
	} else if err != nil {
		return nil, err
	} else {
		dir.config, err = ReadConfig(file)
		file.Close()
		if err != nil {
			return nil, err
		}
	}
	if file, err = os.Open(dir.join("revision")); err == nil {
		_, err = fmt.Fscan(file, &dir.revision)
		file.Close()
		if err != nil {
			return nil, errors.New("invalid revision file")
		}
	} else {
		dir.revision = dir.meta.OldRevision
	}

	return dir, nil
}
开发者ID:jameinel,项目名称:core,代码行数:36,代码来源:dir.go

示例3: ScanPlugins

// Scaning path for finding plugins that contain files
// whith specific suffix
func ScanPlugins(path string, suffix string) []*Plugin {
	var plugins []*Plugin
	f, err := os.Open(path)
	if err != nil {
		log.Warn(err)
		return nil
	}
	defer f.Close()
	dirs, err := f.Readdirnames(-1)
	if err != nil {
		log.Warn(err)
		return nil
	}
	for _, dir := range dirs {
		dir2 := pt.Join(path, dir)
		f2, err := os.Open(dir2)
		if err != nil {
			log.Warn(err)
			continue
		}
		defer f2.Close()
		fi, err := f2.Readdir(-1)
		if err != nil {
			continue
		}
		for _, f := range fi {
			fn := f.Name()
			if strings.HasSuffix(fn, suffix) {
				plugins = append(plugins, NewPlugin(dir2, suffix))
				break
			}
		}
	}
	return plugins
}
开发者ID:hanshenu,项目名称:lime,代码行数:37,代码来源:plugin.go

示例4: Load

func Load() Config {
	file, err := os.Open(Path())

	var wConfig Config

	if err != nil {
		log.Printf("Config file not found. Creating new...")
		err = CreateNewConfigFile()

		if err != nil {
			log.Fatalf("Unable to create new config file. Error: %v", err)
			os.Exit(1)
		}

		file, err = os.Open(Path())
		if err != nil {
			log.Fatalf("Unable to read new config file. Error: %v", err)
			os.Exit(1)
		}
	}

	defer file.Close()

	toml.DecodeReader(file, &wConfig)

	return wConfig
}
开发者ID:sparkymat,项目名称:wain,代码行数:27,代码来源:config.go

示例5: cget

func (c *Conn) cget(name string) (data []byte, err error) {
	cache := filepath.Join(c.cache, name)
	f, err := os.Open(cache)
	if err == nil {
		defer f.Close()
		return ioutil.ReadAll(f)
	}
	if altCache := c.altCachePath(name); altCache != "" {
		f, err := os.Open(altCache)
		if err == nil {
			defer f.Close()
			return ioutil.ReadAll(f)
		}
	}

	data, err = c.bget(name)
	if err != nil {
		return nil, err
	}

	dir, _ := filepath.Split(cache)
	os.MkdirAll(dir, 0700)
	ioutil.WriteFile(cache, data, 0600)
	return data, nil
}
开发者ID:amrhassan,项目名称:rsc,代码行数:25,代码来源:arq.go

示例6: LoadManual

func LoadManual(version string, funcname string) (io.ReadCloser, error) {
	pagename := version + ":" + funcname
	pagename = strings.Replace(pagename, ".", "-", -1)
	pagename = strings.Replace(pagename, "_", "-", -1)
	cachepath := path.Join(cachedir, pagename)
	cachefile, err := os.Open(cachepath)
	if err == nil {
		return cachefile, nil
	}
	os.MkdirAll(cachedir, 0755)
	url := "http://api.zeromq.org/" + pagename
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("%s -> %s", url, resp.Status)
	}
	if cachefile, err = os.Create(cachepath); err != nil {
		return nil, err
	}
	if _, err := io.Copy(cachefile, resp.Body); err != nil {
		return nil, err
	}
	return os.Open(cachepath)
}
开发者ID:Pinkxa,项目名称:gophernotes,代码行数:27,代码来源:main.go

示例7: renameRelation

func renameRelation(c *gc.C, charmPath, oldName, newName string) {
	path := filepath.Join(charmPath, "metadata.yaml")
	f, err := os.Open(path)
	c.Assert(err, jc.ErrorIsNil)
	defer f.Close()
	meta, err := corecharm.ReadMeta(f)
	c.Assert(err, jc.ErrorIsNil)

	replace := func(what map[string]corecharm.Relation) bool {
		for relName, relation := range what {
			if relName == oldName {
				what[newName] = relation
				delete(what, oldName)
				return true
			}
		}
		return false
	}
	replaced := replace(meta.Provides) || replace(meta.Requires) || replace(meta.Peers)
	c.Assert(replaced, gc.Equals, true, gc.Commentf("charm %q does not implement relation %q", charmPath, oldName))

	newmeta, err := goyaml.Marshal(meta)
	c.Assert(err, jc.ErrorIsNil)
	ioutil.WriteFile(path, newmeta, 0644)

	f, err = os.Open(path)
	c.Assert(err, jc.ErrorIsNil)
	defer f.Close()
	_, err = corecharm.ReadMeta(f)
	c.Assert(err, jc.ErrorIsNil)
}
开发者ID:bac,项目名称:juju,代码行数:31,代码来源:util_test.go

示例8: FindMigrations

func (f FileMigrationSource) FindMigrations() ([]*Migration, error) {
	migrations := make([]*Migration, 0)

	file, err := os.Open(f.Dir)
	if err != nil {
		return nil, err
	}

	files, err := file.Readdir(0)
	if err != nil {
		return nil, err
	}

	for _, info := range files {
		if strings.HasSuffix(info.Name(), ".sql") {
			file, err := os.Open(path.Join(f.Dir, info.Name()))
			if err != nil {
				return nil, err
			}

			migration, err := ParseMigration(info.Name(), file)
			if err != nil {
				return nil, err
			}

			migrations = append(migrations, migration)
		}
	}

	// Make sure migrations are sorted
	sort.Sort(byId(migrations))

	return migrations, nil
}
开发者ID:daohoangson,项目名称:go-socialcounters,代码行数:34,代码来源:migrate.go

示例9: InitializeConfig

func InitializeConfig() {
	var err error
	var out io.Writer
	var erw io.Writer

	do = definitions.NowDo()

	if os.Getenv("ERIS_CLI_WRITER") != "" {
		out, err = os.Open(os.Getenv("ERIS_CLI_WRITER"))
		if err != nil {
			fmt.Printf("Could not open: %s\n", err)
			return
		}
	} else {
		out = os.Stdout
	}

	if os.Getenv("ERIS_CLI_ERROR_WRITER") != "" {
		erw, err = os.Open(os.Getenv("ERIS_CLI_ERROR_WRITER"))
		if err != nil {
			fmt.Printf("Could not open: %s\n", err)
			return
		}
	} else {
		erw = os.Stderr
	}

	config.GlobalConfig, err = config.SetGlobalObject(out, erw)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
开发者ID:alexandrev,项目名称:eris-cli,代码行数:33,代码来源:eris.go

示例10: MergeColumnsWithChannel

func MergeColumnsWithChannel() {
	ch1 := make(chan string, 256)
	ch2 := make(chan string, 256)

	f1, err := os.Open(COL1_FILENAME)
	if err != nil {
		log.Fatalln("open col1.txt error:", err)
	}
	defer f1.Close()
	f2, err := os.Open(COL2_FILENAME)
	if err != nil {
		log.Fatalln("open col2.txt error:", err)
	}
	defer f2.Close()

	mf, err := os.Create(MERGED_FILENAME)
	if err != nil {
		log.Fatalln("create merged.txt error:", err)
	}
	defer mf.Close()

	go ScanFile(f1, ch1)
	go ScanFile(f2, ch2)
	for {
		c1, ok1 := <-ch1
		if !ok1 {
			break
		}
		c2, ok2 := <-ch2
		if !ok2 {
			break
		}
		fmt.Fprintf(mf, "%s\t%s\n", c1, c2)
	}
}
开发者ID:Pandaman,项目名称:natural-language-processing100-practice,代码行数:35,代码来源:main.go

示例11: MergeColumns

func MergeColumns() {
	f1, err := os.Open(COL1_FILENAME)
	if err != nil {
		log.Fatalln("open col1.txt error:", err)
	}
	defer f1.Close()
	s1 := bufio.NewScanner(f1)
	f2, err := os.Open(COL2_FILENAME)
	if err != nil {
		log.Fatalln("open col2.txt error:", err)
	}
	defer f2.Close()
	s2 := bufio.NewScanner(f2)

	mf, err := os.Create(MERGED_FILENAME)
	if err != nil {
		log.Fatalln("create merged.txt error:", err)
	}
	defer mf.Close()

	for s1.Scan() && s2.Scan() {
		c1 := s1.Text()
		c2 := s2.Text()
		fmt.Fprintf(mf, "%s\t%s\n", c1, c2)
	}
}
开发者ID:Pandaman,项目名称:natural-language-processing100-practice,代码行数:26,代码来源:main.go

示例12: include

// #include processing.
func (in *Input) include() {
	// Find and parse string.
	tok := in.Stack.Next()
	if tok != scanner.String {
		in.expectText("expected string after #include")
	}
	name, err := strconv.Unquote(in.Stack.Text())
	if err != nil {
		in.Error("unquoting include file name: ", err)
	}
	in.expectNewline("#include")
	// Push tokenizer for file onto stack.
	fd, err := os.Open(name)
	if err != nil {
		for _, dir := range in.includes {
			fd, err = os.Open(filepath.Join(dir, name))
			if err == nil {
				break
			}
		}
		if err != nil {
			in.Error("#include:", err)
		}
	}
	in.Push(NewTokenizer(name, fd, fd))
}
开发者ID:wheelcomplex,项目名称:go-1,代码行数:27,代码来源:input.go

示例13: getfulllist

func getfulllist() {
	fullList = make(map[string]bool)

	d, err := os.Open(storage)
	if err != nil {
		return
	}
	defer d.Close()
	fid, err := d.Readdir(0)
	if err != nil {
		log.Fatalln(err)
	}
	for _, fi := range fid {
		if fi.Mode().IsDir() {
			if curdir, _ := strconv.Atoi(fi.Name()); curdir == 0 {
				continue
			}
			dtbl, err := os.Open(filepath.Join(storage, fi.Name()))
			if err != nil {
				log.Fatalln(err)
			}
			defer dtbl.Close()
			walkfunc := func(path string, f os.FileInfo, err error) error {
				if f.Mode().IsRegular() {
					fullList[path] = true
				}
				return nil
			}
			filepath.Walk(filepath.Join(storage, fi.Name()), walkfunc)
		}
	}
}
开发者ID:novostrim,项目名称:backupenz,代码行数:32,代码来源:main.go

示例14: Read

func (f *QemuLogFile) Read(p []byte) (n int, err error) {
	reader, err := os.Open(f.Name)
	if err != nil {
		return 0, err
	}
	reader.Seek(f.Offset, os.SEEK_SET)

	for {
		n, err = reader.Read(p)
		f.Offset += int64(n)

		if err == io.EOF {
			if f.eof {
				reader.Close()
				return
			}

			time.Sleep(1 * time.Second)
			reader.Close()
			reader, err = os.Open(f.Name)
			if err != nil {
				reader.Close()
				return
			}
			reader.Seek(f.Offset, os.SEEK_SET)
		}
		if err != nil || n != 0 {
			reader.Close()
			return
		}
	}
}
开发者ID:hyperhq,项目名称:runv,代码行数:32,代码来源:qemu_process.go

示例15: TtyReady

func TtyReady() error {
	var err error
	_stdin, err := os.Open("CONIN$")
	if err != nil {
		return err
	}
	_stdout, err := os.Open("CONOUT$")
	if err != nil {
		return err
	}

	stdin = os.Stdin
	stdout = os.Stdout

	os.Stdin = _stdin
	os.Stdout = _stdout

	syscall.Stdin = syscall.Handle(os.Stdin.Fd())
	err = setStdHandle(syscall.STD_INPUT_HANDLE, syscall.Stdin)
	if err != nil {
		return err
	}
	syscall.Stdout = syscall.Handle(os.Stdout.Fd())
	err = setStdHandle(syscall.STD_OUTPUT_HANDLE, syscall.Stdout)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:heavenshell,项目名称:peco,代码行数:30,代码来源:tty_windows.go


注:本文中的os.Open函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。