From f6f8b0d8062c768608eb2a41afb01d41db33f9f8 Mon Sep 17 00:00:00 2001 From: VIVIMAN Date: Fri, 31 Dec 2021 17:22:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=8D=E5=8A=A1=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- golang_learn/data_func/go_to_windows/Main.go | 66 ++++++++++++++++++++ golang_learn/go.mod | 2 +- golang_learn/go.sum | 5 +- 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 golang_learn/data_func/go_to_windows/Main.go diff --git a/golang_learn/data_func/go_to_windows/Main.go b/golang_learn/data_func/go_to_windows/Main.go new file mode 100644 index 0000000..ffa4122 --- /dev/null +++ b/golang_learn/data_func/go_to_windows/Main.go @@ -0,0 +1,66 @@ +package main + +import ( + "github.com/kardianos/service" + "log" + "os" + "time" +) + +type program struct{} + +func (p *program) Start(s service.Service) error { + go p.run() + return nil +} + +func (p *program) run() { + for { + time.Sleep(time.Second) + log.Println("running") + } +} + +func (p *program) Stop(s service.Service) error { + return nil +} + +func init() { + f, err := os.Create("d:/gowinservice.txt") + if err != nil { + log.Fatal(err) + } + log.SetOutput(f) +} + +func main() { + svcConfig := &service.Config{ + Name: "GoService", + DisplayName: "GoServiceDis", + Description: "windows service form golang", + } + + prg := &program{} + s, err := service.New(prg, svcConfig) + if err != nil { + log.Fatal(err) + } + + if len(os.Args) > 1 { + if os.Args[1] == "install" { + s.Install() + log.Println("服务安装成功") + return + } + + if os.Args[1] == "remove" { + s.Uninstall() + log.Println("服务卸载成功") + return + } + } + + if err = s.Run(); err != nil { + log.Fatal(err) + } +} diff --git a/golang_learn/go.mod b/golang_learn/go.mod index b63fb5c..8aa8511 100644 --- a/golang_learn/go.mod +++ b/golang_learn/go.mod @@ -16,13 +16,13 @@ require ( github.com/google/martian v2.1.0+incompatible github.com/google/uuid v1.1.2 github.com/joho/godotenv v1.3.0 + github.com/kardianos/service v1.2.0 github.com/lxn/walk v0.0.0-20210112085537-c389da54e794 github.com/lxn/win v0.0.0-20210218163916-a377121e959e github.com/mattn/go-adodb v0.0.1 github.com/mattn/go-oci8 v0.1.1 github.com/mattn/go-sqlite3 v1.14.8 github.com/russross/blackfriday v1.6.0 - github.com/satori/go.uuid v1.2.0 github.com/spf13/viper v1.8.1 github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb golang.org/x/exp/errors v0.0.0-20210715201039-d37aa40e8013 diff --git a/golang_learn/go.sum b/golang_learn/go.sum index a29ac3d..0fcbda2 100644 --- a/golang_learn/go.sum +++ b/golang_learn/go.sum @@ -206,6 +206,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/kardianos/service v1.2.0 h1:bGuZ/epo3vrt8IPC7mnKQolqFeYJb7Cs8Rk4PSOBB/g= +github.com/kardianos/service v1.2.0/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= @@ -263,8 +265,6 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww= github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= -github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= -github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= @@ -459,6 +459,7 @@ golang.org/x/sys v0.0.0-20200720211630-cb9d2d5c5666/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201018230417-eeed37f84f13/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=