diff --git a/g/database/gkafka/gkafka.go b/g/database/gkafka/gkafka.go index c8f3f3de8..8121852b3 100644 --- a/g/database/gkafka/gkafka.go +++ b/g/database/gkafka/gkafka.go @@ -200,7 +200,7 @@ func (client *Client) SyncSend(message *Message) error { return nil } -// Send data to kafka in asynchronized way. +// Send data to kafka in asynchronized way(concurrent safe). func (client *Client) AsyncSend(message *Message) error { if client.asyncProducer == nil { if p, err := sarama.NewAsyncProducer(strings.Split(client.Config.Servers, ","), &client.Config.Config); err != nil { diff --git a/g/os/gfile/gfile_contents.go b/g/os/gfile/gfile_contents.go index 26cb3d737..a65c4a702 100644 --- a/g/os/gfile/gfile_contents.go +++ b/g/os/gfile/gfile_contents.go @@ -15,7 +15,9 @@ import ( const ( // 方法中涉及到读取的时候的缓冲大小 - gREAD_BUFFER = 1024 + gREAD_BUFFER = 1024 + // 方法中涉及到文件指针池的默认缓存时间(秒) + gFILE_POOL_EXPIRE = 60 ) // (文本)读取文件内容 @@ -88,10 +90,10 @@ func GetNextCharOffset(file *os.File, char byte, start int64) int64 { if n, err := file.ReadAt(buffer, offset); n > 0 { for i := 0; i < n; i++ { if buffer[i] == char { - return int64(i) + return int64(i) + offset } } - offset++ + offset += int64(n) } else if err != nil { break } @@ -99,10 +101,32 @@ func GetNextCharOffset(file *os.File, char byte, start int64) int64 { return -1 } -// 获得文件内容直到下一个指定字节的位置(返回值不包含该位置字符内容) +// 获得文件内容下一个指定字节的位置 +func GetNextCharOffsetByPath(path string, char byte, start int64) int64 { + if f, err := gfpool.Open(path, os.O_RDONLY, 0666, gFILE_POOL_EXPIRE*1000); err == nil { + defer f.Close() + return GetNextCharOffset(&f.File, char, start) + } else { + // panic(err) + } + return -1 +} + +// 获得文件内容直到下一个指定字节的位置(返回值包含该位置字符内容) func GetBinContentsTilChar(file *os.File, char byte, start int64) ([]byte, int64) { if offset := GetNextCharOffset(file, char, start); offset != -1 { - return GetBinContentsByTwoOffsets(file, start, offset), offset + return GetBinContentsByTwoOffsets(file, start, offset + 1), offset + } + return nil, -1 +} + +// 获得文件内容直到下一个指定字节的位置(返回值包含该位置字符内容) +func GetBinContentsTilCharByPath(path string, char byte, start int64) ([]byte, int64) { + if f, err := gfpool.Open(path, os.O_RDONLY, 0666, gFILE_POOL_EXPIRE*1000); err == nil { + defer f.Close() + return GetBinContentsTilChar(&f.File, char, start) + } else { + // panic(err) } return nil, -1 } @@ -114,4 +138,15 @@ func GetBinContentsByTwoOffsets(file *os.File, start int64, end int64) []byte { return nil } return buffer +} + +// 获得文件内容中两个offset之间的内容 [start, end) +func GetBinContentsByTwoOffsetsByPath(path string, start int64, end int64) []byte { + if f, err := gfpool.Open(path, os.O_RDONLY, 0666, gFILE_POOL_EXPIRE*1000); err == nil { + defer f.Close() + return GetBinContentsByTwoOffsets(&f.File, start, end) + } else { + // panic(err) + } + return nil } \ No newline at end of file diff --git a/g/os/gfile/gfile_size.go b/g/os/gfile/gfile_size.go index ec5075757..ad82aadf6 100644 --- a/g/os/gfile/gfile_size.go +++ b/g/os/gfile/gfile_size.go @@ -13,11 +13,11 @@ import ( // 文件大小(bytes) func Size(path string) int64 { - f, e := os.Stat(path) + s, e := os.Stat(path) if e != nil { return 0 } - return f.Size() + return s.Size() } // 格式化文件大小 diff --git a/g/os/gfpool/gfpool.go b/g/os/gfpool/gfpool.go index 919fd813e..6bf38aef0 100644 --- a/g/os/gfpool/gfpool.go +++ b/g/os/gfpool/gfpool.go @@ -33,7 +33,7 @@ type File struct { // 全局指针池,expire < 0表示不过期,expire = 0表示使用完立即回收,expire > 0表示超时回收 var pools = gmap.NewStringInterfaceMap() -// 获得文件对象,并自动创建指针池 +// 获得文件对象,并自动创建指针池(过期时间单位:毫秒) func Open(path string, flag int, perm os.FileMode, expire...int) (*File, error) { fpExpire := 0 if len(expire) > 0 { @@ -54,6 +54,7 @@ func OpenFile(path string, flag int, perm os.FileMode, expire...int) (*File, err } // 创建一个文件指针池,expire = 0表示不过期,expire < 0表示使用完立即回收,expire > 0表示超时回收,默认值为0不过期 +// 过期时间单位:毫秒 func New(path string, flag int, perm os.FileMode, expire...int) *Pool { fpExpire := 0 if len(expire) > 0 { diff --git a/geg/os/gfile/gfile_contents.go b/geg/os/gfile/gfile_contents.go index ecf913a06..f6f5ac31a 100644 --- a/geg/os/gfile/gfile_contents.go +++ b/geg/os/gfile/gfile_contents.go @@ -9,11 +9,10 @@ func main() { path := "/tmp/temp" content := `123 456 -789` +789 +` gfile.PutContents(path, content) - f, err := gfile.Open(path) - if err != nil { - panic(err) - } - fmt.Println(gfile.GetBinContentsTilChar(f, '\n', 0)) + fmt.Println(gfile.GetBinContentsTilCharByPath(path, '\n', 0)) + fmt.Println(gfile.GetBinContentsTilCharByPath(path, '\n', 3)) + fmt.Println(gfile.GetBinContentsTilCharByPath(path, '\n', 8)) } diff --git a/geg/os/gproc/gproc_shellexec.go b/geg/os/gproc/gproc_shellexec.go index 8abb43624..6c10d531d 100644 --- a/geg/os/gproc/gproc_shellexec.go +++ b/geg/os/gproc/gproc_shellexec.go @@ -7,6 +7,7 @@ import ( // 执行shell指令 func main () { - r, err := gproc.ShellExec("sleep 3s; echo 'hello';") - fmt.Println("result:", r, err) + r, err := gproc.ShellExec(`sleep 3s; echo "hello gf!";`) + fmt.Println("result:", r) + fmt.Println(err) } diff --git a/geg/other/test.go b/geg/other/test.go index db073169d..14847be34 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,10 +2,13 @@ package main import ( "fmt" - "gitee.com/johng/gf/g/os/gfile" - "gitee.com/johng/gf/g/os/gtime" ) -func main() { - fmt.Println(gtime.NewFromTimeStamp(gfile.MTime("/home/john/Documents/temp")).String()) +func test() { + defer fmt.Println(1) + fmt.Println(2) +} + +func main() { + test() } diff --git a/geg/util/gregex/gregex.go b/geg/util/gregex/gregex.go new file mode 100644 index 000000000..2eebd54de --- /dev/null +++ b/geg/util/gregex/gregex.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + "gitee.com/johng/gf/g/util/gregex" +) + +func main() { + match, _ := gregex.MatchString(`(\w+).+\-\-\s*(.+)`, `GF is best! -- John`) + fmt.Printf(`%s says "%s" is the one he loves!`, match[2], match[1]) +}