文件结构
/home/vagrant/gocode/gtail
├── go.mod
├── go.sum
├── log_watcher.go
├── main.go
└── playbooks
├── file_watcher
│ └── compile.json
└── session.yaml
log_watcher 的内容
package log_watcher
import (
"fmt"
"log"
"os"
"path/filepath"
)
type LogWatcher struct {
Pattern string
}
func (c *LogWatcher) tail() {
matches, err := filepath.Glob(c.Pattern)
if err != nil {
log.Fatal(err)
return
}
for k, v := range matches {
file_obj, err := os.Open(v)
if err != nil {
log.Fatal(err)
return
}
go func() {
buf := make([]byte, 1024)
for {
n, _ := file_obj.Read(buf)
if 0 == n {
break
}
fmt.Print(buf[:n])
}
}()
}
return
}
main.go
的内容
package main
import (
"fmt"
"log"
"os"
"github.com/urfave/cli"
"gtail/log_watcher"
)
func main() {
var pattern string
app := &cli.App{
Name: "gtail",
Usage: "golang tail",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pattern",
Usage: "language for the greeting",
Required: true,
Destination: &pattern,
},
},
Action: func(c *cli.Context) error {
if len(pattern) == 0 {
fmt.Printf("miss pattern\n")
return nil
}
var log_watcher = &LogWatcher{
Pattern: pattern,
}
log_watcher.tail()
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
为什么 build 的时候提示
can't load package: package gtail: found packages log_watcher (log_watcher.go) and main (main.go) in /home/vagrant/gocode/gtail
我另一个项目这么导包又成功了,感觉 golang 的导包真的玄学,
1
warlock 2020-08-07 17:06:07 +08:00 1
同一个目录下 包名要相同
|
3
useben 2020-08-07 17:48:47 +08:00 1
你用记事本写的?
|
4
raaaaaar 2020-08-07 18:21:57 +08:00 via Android
gomod 不是很好用么。
|
6
chenqh OP md, golang 版的内存比 python 还高, 我也是醉了
|