i am using shell with golang to access apache log file and get some data. first i used to write output to the file directly and it was working, but now i need to get the output and use it in the program directly. and also i need to convert it to float64. i tried converting it into a string and then to float64, but it is not working?
func Mem_usage_data(j int) (Mem_predict float64, err error) {
awkPart := fmt.Sprintf("awk '{print $%d/1024}'", j)
out1, err := exec.Command("bash", "-c", "tail -n 1 /var/log/apache2/access.log| "+awkPart+" ").Output()
fmt.Println("memory usage is", out1)
s1 := string(out1)
v1, err1 := strconv.ParseFloat(s1, 64)
if err1 != nil {
fmt.Println(err)
}
if err != nil {
fmt.Println(err)
}
return v1, err
}
when i print the out1 i get something like this [48 46 49 50 48 49 49 55 10]. CAn you please help how to get the exact output in out1 and how to convert it into float64?
The conversion fails probably because there is some whitespace or newline character on s1
. Try to trim it first before doing the conversion. Use strings.TrimSpace()
to achieve that.
v1, err1 := strconv.ParseFloat(strings.TrimSpace(s1), 64)