Skip to content

Commit e187503

Browse files
author
norbertwagner
committed
project init
0 parents  commit e187503

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

app/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("Hello World! 1")
7+
fmt.Println("Hello World! 2")
8+
fmt.Println("Hello World! 3")
9+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module godemon
2+
3+
go 1.15

main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"time"
8+
)
9+
10+
func watchFile(filePath string) error {
11+
initialStat, err := os.Stat(filePath)
12+
if err != nil {
13+
return err
14+
}
15+
16+
for {
17+
stat, err := os.Stat(filePath)
18+
if err != nil {
19+
return err
20+
}
21+
22+
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {
23+
break
24+
}
25+
26+
time.Sleep(1 * time.Second)
27+
}
28+
29+
return nil
30+
}
31+
32+
func main() {
33+
doneChan := make(chan bool)
34+
for true {
35+
go func(doneChan chan bool) {
36+
defer func() {
37+
doneChan <- true
38+
}()
39+
40+
err := watchFile("./app/main.go")
41+
if err != nil {
42+
fmt.Println(err)
43+
}
44+
45+
fmt.Println("File has been changed")
46+
cmd := exec.Command("go", "run", "./app/main.go")
47+
cmd.Stdout = os.Stdout
48+
cmd.Stderr = os.Stderr
49+
cmd.Run()
50+
}(doneChan)
51+
52+
<-doneChan
53+
}
54+
}

0 commit comments

Comments
 (0)