Tag: web service

A great feature of scripting languages like PHP, Python and Ruby is that you don’t need to re-compile the app or restart a web server every time you change something. With Go, you need to restart the web server for your changes to take affect. This can be a pretty daunting task.

We can, however, have this feature in Go as well (with some extra code). We just need to write a file watcher that will restart the web server on any file changes. Below is working code (from a project I am building) that does exactly this. You can modify it to suit your needs or just put it in your project as is.

Go

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

packagemain

// This file is: web/main_dev.go

// It will recursively monitor any path we are interested in and re-start the web server

// on any file changes

//

// web/main.go is where my web server code resides. You should change the code

// to your run the command you use for your web server in main()

import(

"os/exec"

"path/filepath"

"os"

"crypto/md5"

"io"

"encoding/hex"

"time"

"fmt"

)

// We will store all MD5 hashes of files we are interested in, in this variable