Want to call my all functions in a certain interval and make one file for all my package for one exe


(uday kumar) #1

I am new with go language and working with my first application.

I have my few packages and from that, I am printing data.

  1. I want to print the same data with a certain interval.

  2. Call all the packages in single go file, so that I can make one .exe.

I have pushed my initial code here and I wrote questions for help.

Git repository link


(Ashish Shekar) #2

Solution for:

  1. Take a look at tickers - https://gobyexample.com/tickers. This is what you’ll need to run functions in intervals.
  2. I see you are using a single import statement so you will have to gather all of the code which is scattered in each file into a single file.

eg; main.go

package main

import (
    "fmt"

    // addition: from net.go
    "github.com/shirou/gopsutil/net"
    "github.com/shirou/gopsutil/winservices"
    ps "github.com/mitchellh/go-ps"
    "golang.org/x/sys/windows/svc"
)

func main() {
    listServices, _ := winservices.NewService("")
    fmt.Printf("All listServices: ", listServices)


    processList, _ := ps.Processes()
    fmt.Printf("All ps: ", processList)

    for x := range processList {
        var process ps.Process
        process = processList[x]
        fmt.Printf("%d\t%s\n",process.Pid(),process.Executable())
    }

    statusHandler, _ := svc.StatusHandle()
    fmt.Printf("All statusHandler: ", statusHandler)
    
    // addition: from net.go
    infoIOCounter, _ := net.IOCounters(true)
    fmt.Printf("All net info: ", infoIOCounter)
}

then run go build.

NOTE: there are several different ways to structure your code but this seems to be the simplest one

Mostly start with working on moving all code into one and start implementing tickers.


(uday kumar) #3

Thanks for your reply

Here in each file I have main function and that I do not want.

How I can call all file in a single new file like main.go


(Ashish Shekar) #4

Change the name of the main function in all the file except the main.go file and then call each function in your main.go func main() directly as it is in the same package.

this needs changes in your build command. So in go build you have to paas name of all files to create a single exe. eg: go build main.go net.go ... and so on.