How can i call the getuptime function from main to extract uptime information?

package main

import (
	"time"
	"os"
	"bufio"
	"strings"
	"log"
	"strconv"
	"fmt"

)




func GetUptime() float64 {

	for {
		file, err := os.Open("/proc/uptime")
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			text := scanner.Text()
			parts := strings.Split(text, " ")
			parts1 := parts[0]
			uptime, _ := strconv.ParseFloat(parts1, 64)
			 
			}



			   	}

		time.Sleep(5 * time.Second)

	return 
	}



func main() {
	            
	    


}

You can call it as you call every other function as well: GetUptime("").

I’m not sure though, what that string argument is for, you do not use it in the function…

You don’t use procfile anywhere. What is it mean to be?

Sorry it was a wrong code. Now i have edited it so please view it again

Not much different to what I have written before, but you can leave off the argument now: GetUptime().

But if you do not know how to call a function, you really should take the tour, this is a very basic and common task to do. You actually called functions from inside the GetUptime function, and I do not understand how calling GetUptime() is different to calling os.Open("/proc/uptime").

2 Likes

In this code i am trying to extract the first uptime information i.e parts[0] which can be seen in the terminal using command $ cat proc/uptime and which will be updated in every 5 sec.
How can i return the uptime value after pasing to float64 in GetUptime() and call it in main to display it ?

For display you probably need fmt.Println() or fmt.Printf().

How you can call your function, I already told you, but in your function you are never returning something but 0.0.

You probably want to have return uptime, but I’m not sure where exactly you want that, as you are reading the uptime in a loop and therefore potentially have multiple values, but you are only able to return a single one of those.

Also I am not sure why you have the time.Sleep() in that function.

I get an error message saying unresolved refrence while returning uptime value in that function ?
and i have applied fmt.Println() too
The time.sleep() returns the updated uptime information in every 5 sec

If i write the above code of GetUptime function to the main function by replacing uptime, _ := strconv.ParseFloat(parts1, 64) by fmt.Println (“parts[0]”) then it will run
but i want to call that function in the main function

package main

import (
	"time"
	"os"
	"bufio"
	"strings"
	"log"
	"strconv"
	"fmt"

)




func GetUptime() uptime {

	
	for {
		file, err := os.Open("/proc/uptime")
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			text := scanner.Text()
			parts := strings.Split(text, " ")
			parts1 := parts[0]
			uptime, _ := strconv.ParseFloat(parts1, 64)

			}



			   	}

		time.Sleep(5 * time.Second)

	return uptime
	}



func main() {
	            
	     fmt.Println("", GetUptime())


}

What is the exact error message?

No it doesn’t, actually it is never reached at all.

Your function runs in an infinite loop, the return and the time.Sleep() are never reached.

  1. remove the outer loop.
  2. remove the sleep.
  3. return an actual value (probably uptime)
  4. check for strconv.ParseFloat()s returned error
  5. use go fmt
  6. do the looping in main
  7. do the time.Sleep() in the loop in main

In general: clean up your code

2 Likes

Thank you It works
But doing the time.sleep() in the loop in main gives the multiple number of immediate output in a second

Then you are not doing it correctly.

func main() {
  for {
    fmt.Println(GetUptime())
    time.Sleep(5 * time.Second)
  }
}

This is how I would use it from main, assuming you have repaired your GetUptime(). If you have it like this, but are getting multiple output in a single second, your GetUptime is still broken.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.