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


Golang File.Read方法代码示例

本文整理汇总了Golang中os.File.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang File.Read方法的具体用法?Golang File.Read怎么用?Golang File.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os.File的用法示例。


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

示例1: Md5file

func Md5file(path string) (hash_hex string, err error) {
	var file *os.File
	if file, err = os.OpenFile(path, os.O_RDONLY, 0644); err != nil {
		return
	}
	defer file.Close()

	h := md5.New()
	//io.Copy(h, file)
	var data []byte = make([]byte, 8192)
	var size int
	for {
		size, err = file.Read(data)
		if err != nil {
			break
		}
		h.Write(data[:size])
		h.Sum(nil)
	}
	if err != nil && err != io.EOF {
		return
	}
	hash_hex = fmt.Sprintf("%x", h.Sum(nil))
	err = nil
	return
}
开发者ID:first-developer,项目名称:golang-utils,代码行数:26,代码来源:md5.go

示例2: GetChar

// GetChar reads a single byte.
func GetChar(f *os.File) (b byte, err error) {
	bs := make([]byte, 1, 1)
	if _, err = f.Read(bs); err != nil {
		return 0, err
	}
	return bs[0], err
}
开发者ID:postfix,项目名称:goterm-1,代码行数:8,代码来源:termios.go

示例3: dataLookup

func (wndb *WordNetDb) dataLookup(fh *os.File, offset int64) ([]byte, error) {
	_, err := fh.Seek(offset, os.SEEK_SET)
	if err != nil {
		return nil, err
	}

	buffer := make([]byte, BUFFSIZE) // initial size of the buffer is 3kb
	line := make([]byte, 0, BUFFSIZE)
	prevLen := 0
	for {
		prevLen = len(line)
		n, err := fh.Read(buffer) // we read the next 3kb (or less)
		if err != nil && err != io.EOF {
			return nil, err
		}
		line = append(line, buffer[:]...)
		until := bytes.IndexByte(buffer, '\n')
		if until > 0 { // We have a full line
			return line[:prevLen+until], nil
		}
		if err == io.EOF || n < BUFFSIZE {
			return line, nil
		}
	}
	return nil, ERR_MSG(UNREACHABLE_CODE)
}
开发者ID:emepyc,项目名称:gown,代码行数:26,代码来源:search.go

示例4: LoadConfig

func LoadConfig(fname string) (cfg Config, err error) {
	var fd *os.File
	var fs os.FileInfo
	var data []byte

	// Check if the config file exists, and create a buffer to hold it's content
	if fs, err = os.Stat(fname); err != nil {
		return
	}
	data = make([]byte, fs.Size())

	// Open and read the file into the buffer
	if fd, err = os.Open(fname); err != nil {
		return
	}
	defer fd.Close()

	if _, err = fd.Read(data); err != nil {
		return
	}

	// Parse the yaml into a struct
	cfg = Config{}

	err = yaml.Unmarshal(data, &cfg)
	if err != nil {
		return
	}

	// Set the hostname
	cfg.Hostname, _ = os.Hostname()

	return
}
开发者ID:r3boot,项目名称:logshipper,代码行数:34,代码来源:config.go

示例5: main

func main() {
	var src, dst *os.File
	src, _ = os.Open("base64")
	defer func() {
		src.Close()
		dst.Close()
	}()

	dst, _ = os.OpenFile("base64_2", os.O_WRONLY|os.O_CREATE, 0644)

	//_, err := io.Copy(dst, src)
	//fmt.Println(err)
	buf := make([]byte, 1024)
	for {
		nr, er := src.Read(buf)
		if nr > 0 {
			nw, _ := dst.Write(buf[0:nr])
			fmt.Println("write size:", nw)
		}
		if er == io.EOF {
			break
		}
	}

}
开发者ID:needkane,项目名称:qiniu,代码行数:25,代码来源:noCopy.go

示例6: ReadPassword

func ReadPassword() ([]byte, error) {
	var err error
	var data []byte = make([]byte, KeySize)
	var tty *os.File

	if tty, err = os.Open("/dev/tty"); err != nil {
		return nil, err
	}

	C.savetermios(C.int(os.Stdin.Fd()))
	C.setnoecho(C.int(os.Stdin.Fd()))

	_, err = tty.Read(data)

	C.resettermios(C.int(os.Stdin.Fd()))

	if err != nil {
		return nil, err
	}

	tty.Close()

	fmt.Fprintln(os.Stderr)

	for k, c := range data {
		if c == '\n' {
			data[k] = 0
		}
	}

	return data, nil
}
开发者ID:mytchel,项目名称:pass,代码行数:32,代码来源:password.go

示例7: LoadKeys

func (km *KeyManager) LoadKeys(file *os.File) (err error) {
	//load file
	km.keyPair = new(common.PrivateKey)
	km.keyPair.AllocateNew()

	n, err := file.Read(km.keyPair.Key[:])
	if err == nil {
		if n != 64 {
			err = errors.New(" n != ed25519.PrivateKeySize ")
		}
	}

	if err != nil {
		return
	}

	n, err = file.Read(km.keyPair.Pub.Key[:])
	if err == nil {
		if n != 32 {
			err = errors.New(" n != ed25519.PublicSize ")
		}
	}

	return
}
开发者ID:6londe,项目名称:FactomCode,代码行数:25,代码来源:keymanager.go

示例8: rawReadline

// rawReadline tries to return a single line, not including the end-of-line
// bytes with raw Mode (without prompting nothing). Or if provided show some
// value instead of actual value.
func (i *UI) rawReadline(f *os.File) (string, error) {
	var resultBuf []byte
	for {
		var buf [1]byte
		n, err := f.Read(buf[:])
		if err != nil && err != io.EOF {
			return "", err
		}

		if n == 0 || buf[0] == '\n' || buf[0] == '\r' {
			break
		}

		if buf[0] == 3 {
			return "", ErrInterrupted
		}

		if i.mask {
			fmt.Fprintf(i.Writer, i.maskVal)
		}

		resultBuf = append(resultBuf, buf[0])
	}

	fmt.Fprintf(i.Writer, "\n")
	return string(resultBuf), nil
}
开发者ID:tcnksm,项目名称:go-input,代码行数:30,代码来源:read.go

示例9: openRegion

func openRegion(f *os.File, writeable bool, offset int64) (*region, error) {
	_, err := f.Seek(offset, os.SEEK_SET)
	if err != nil {
		return nil, err
	}
	header := make([]byte, regionHeaderSize)
	_, err = f.Read(header)
	if err != nil {
		return nil, err
	}
	if bytes.Compare(header[:8], signature) != 0 {
		return nil, InvalidSignature
	}
	size := *(*int64)(unsafe.Pointer(&header[regionSizeOffset]))
	rid := *(*int64)(unsafe.Pointer(&header[regionId]))
	flags := syscall.PROT_READ
	if writeable {
		flags |= syscall.PROT_WRITE
	}
	d, err := syscall.Mmap(int(f.Fd()), offset, int(size), flags, syscall.MAP_SHARED)
	if err != nil {
		return nil, err
	}
	r := &region{
		id:                 rid,
		f:                  f,
		d:                  d,
		freePtr:            d[regionFreePointerOffset : regionFreePointerOffset+8],
		blockListNextIdPtr: d[len(d)-8 : len(d)],
	}
	return r, nil
}
开发者ID:alecthomas,项目名称:vheap,代码行数:32,代码来源:region.go

示例10: UndoCmd

func UndoCmd(args []string) (err error) {
	if len(args) != 0 {
		return errors.New("Usage: gorf [flags] undo")
	}

	lastChangePath := filepath.Join(LocalRoot, ".change.0.gorfc")

	var srcFile *os.File
	srcFile, err = os.Open(lastChangePath)
	if err != nil {
		return
	}

	buf := make([]byte, 1024)
	var n int
	n, err = srcFile.Read(buf)
	fmt.Printf("Undoing \"%s\"\n", strings.TrimSpace(string(buf[:n])))

	filepath.Walk(LocalRoot, undoscanner(0).Walk)

	ur := UndoRoller{incr: -1}
	filepath.Walk(LocalRoot, ur.Walk)
	return ur.err

	return
}
开发者ID:rwl,项目名称:gorf,代码行数:26,代码来源:undo.go

示例11: ReadAChunk

func ReadAChunk(inFile *os.File) int {
	// make a buffer to keep chunks that are read
	if LastBlockSent {
		return 0
	}
	LastBlockSent = false
	chunk.blocknum++
	if chunk.blocknum > 0 {
		mybuf := make([]byte, 512)
		// read a chunk
		fmt.Println("About to read Block num ", chunk.blocknum, "the handle is", inFile, "END OF HANDLE***************************")
		n, err := inFile.Read(mybuf)
		if err != nil && err != io.EOF {
			panic(err)
		}
		chunk.nbytes = n
		chunk.buf = mybuf
		if n < 512 {
			LastBlockSent = true
		}
		return n
	} else {
		return 512
	}
}
开发者ID:meitros,项目名称:tftp-go,代码行数:25,代码来源:tftpd.go

示例12: readLine

func readLine(file *os.File, bytes []byte) (int, error) {
	var i int
	b := make([]byte, 1)
	escaped := false

	for i = 0; i < len(bytes); i++ {
		_, err := file.Read(b)
		if err != nil {
			return i, err
		}

		if rune(b[0]) == '\\' {
			escaped = true
		} else if rune(b[0]) == '\n' {
			if escaped {
				escaped = false
				i -= 2
				continue
			} else {
				break
			}
		} else {
			escaped = false
		}

		bytes[i] = b[0]
	}

	return i, nil
}
开发者ID:mytchel,项目名称:asthum,代码行数:30,代码来源:asthum.go

示例13: readContents

func readContents(file *os.File, length int32) {
	data := make([]byte, length)
	_, err := file.Read(data)
	if err != nil {
		log.Fatal(err)
	}
}
开发者ID:WyriMaps,项目名称:AdtExtractor,代码行数:7,代码来源:reader.go

示例14: Load

func (b *Buffer) Load(q0 uint, fd *os.File, nulls *int) int {
	if q0 > b.nc {
		panic("internal error: buffer.Load")
	}

	p := make([]byte, MaxBlock+utf8.UTFMax+1)

	m := 0
	n := 1
	q1 := q0
	var err error

	for n > 0 {
		n, err = fd.Read(p[m:])
		if err != nil {
			panic(err)
		}
		m += n
		l := m
		if n > 0 {
			l -= utf8.UTFMax
		}
		r := bytes.Runes(p[:l])
		nr := len(r)
		//nb := len([]byte(string(r)))
		copy(p, p[:m])
		b.Insert(q1, uint(nr), r)
		q1 += uint(nr)
	}
	return int(q1 - q0)
}
开发者ID:rjkroege,项目名称:acme,代码行数:31,代码来源:buf.go

示例15: maxIpInPart

func maxIpInPart(part *os.File) (ip3 int, max int) {
	ip3Array := make([]int, 1<<24, 1<<24)
	b := make([]byte, 1<<16*3)
	part.Seek(0, 0)
	for {
		n, err := part.Read(b)
		if n > 0 {
			for i := 0; i < n; i += 3 {
				ip3Array[int(Ip3BytesToUint32(b[i:i+3]))]++
			}

		}
		if err != nil {
			break
		}
	}

	for i := 0; i < len(ip3Array); i++ {
		if ip3Array[i] > max {
			max = ip3Array[i]
			ip3 = i
		}
	}
	return
}
开发者ID:hardPass,项目名称:log100g,代码行数:25,代码来源:logMaxIP_notfinished.go


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