Please first describe what this C code is doing. And then show us what you have tried to convert it to Go. Does your code work? If not, how does it fail?
buf := make([]byte, 1024)
for {
n, err := file.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n==0 {
break
}
}
This is all i tried to convert it.
How can i extract the information from that file system and store it in buffer so that i can use it to get the line started with cpu MHz.
i am working on a project to make monitoring tool. So in /proc/cpuinfo i want to extract the information where the line starts with vendor_id, model name , cpu MHz and so on
I am also new for go programming language. So i was wondering if you could help me
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
func main() {
// Open /proc/cpuinfo for reading, close it after we are done.
file, err := os.Open("/proc/cpuinfo")
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Create a scanner that reads line by line.
scanner := bufio.NewScanner(file)
// Iterate over the lines.
for scanner.Scan() {
line := scanner.Text()
// If the line has the prefix "cpu MHz", print the line.
if strings.HasPrefix(line, "cpu MHz") {
fmt.Println(line)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}