• 93_Go基础_1_61 偏移量 seek


     1 package main
     2 
     3 import (
     4     "fmt"
     5     "io"
     6     "log"
     7     "os"
     8 )
     9 
    10 func main() {
    11     /*
    12         Seek(offset int64, whence int) (int64, error),设置指针光标的位置
    13             第一个参数:偏移量
    14             第二个参数:如何设置
    15                 0:seekstart,表示相对于文件开始
    16                 1:seekcurrent,表示相对于当前位置的偏移量
    17                 2:seekend,表示相对于末尾
    18 
    19 
    20                 // Seek whence values.
    21         const (
    22             SeekStart   = 0 // seek relative to the origin of the file
    23             SeekCurrent = 1 // seek relative to the current offset
    24             SeekEnd     = 2 // seek relative to the end
    25         )
    26     */
    27     fileName := "D:\\Go\\web2\\aa.txt"
    28     file, err := os.OpenFile(fileName, os.O_RDWR, os.ModePerm)
    29     if err != nil {
    30         log.Fatal(err)
    31     }
    32     defer file.Close()
    33 
    34     // 读写
    35     bs := []byte{0}
    36     file.Read(bs)
    37     fmt.Println(string(bs)) // a
    38 
    39     file.Seek(4, io.SeekStart)
    40     file.Read(bs)
    41     fmt.Println(string(bs)) // e
    42 
    43     file.Seek(2, 0) // SeekStart
    44     file.Read(bs)
    45     fmt.Println(string(bs)) // c
    46 
    47     file.Seek(3, io.SeekCurrent)
    48     file.Read(bs)
    49     fmt.Println(string(bs)) // g
    50 
    51     file.Seek(0, io.SeekEnd)
    52     file.WriteString("ABC") // 末尾追加
    53 }
  • 相关阅读:
    一句话开启XP_CMDSHELL
    CF14B Young Photographer 题解
    sql 存储过程与函数区别
    sql索引
    分区表中毒,重装系统
    面试
    XML范例的应用(转载)
    数据结构题目
    网页加载速度的方法和技巧
    设计模式分类
  • 原文地址:https://www.cnblogs.com/luwei0915/p/15661062.html
Copyright © 2020-2023  润新知