Change the c code to the go code

Can you please change this C code to go code ?

#include <stdio.h>
#include <string.h>

float get_cpu_clock_speed () {

FILE* fp ;
char buffer [1024];
size_t bytes_read;
char* match ;
float clock_speed ;

fp = fopen ("/proc/cpuinfo", "r");
bytes_read = fread(buffer, 1, sizeof (buffer), fp);
fclose (fp);

if (bytes_read == 0 || bytes_read == sizeof (buffer))
	return 0;

buffer[bytes_read] = ’\0’;
match = strstr (buffer, "cpu MHz");
if (match == NULL)
	return 0;
sscanf (match, "cpu MHz : %f", &clock_speed);
return clock_speed;

}

int main()
{
printf(β€œCPU clock speed: %4.0f MHz\n”, get_cpu_clock_speed() );
return 0;
}

No.

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?

1 Like

This code is retreiving the information about the cpu that is created on /proc/cpuinfo filesystem in Linux where the line starts with cpu MHz.

func get_cpu_clock_speed () {
file, err := os.Open(/proc/cpuinfo)
if err != nil {
log.Fatal(err)
}
defer file.Close()

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.

So you basically want to execute this?

$ cat /proc/cpuinfo | grep 'cpu MHz'
cpu MHz         : 800.000
cpu MHz         : 800.000
cpu MHz         : 800.000
cpu MHz         : 800.000

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

Try this:

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)
	}
}
1 Like

It works
Thank you so much

I hope you understand what the code does. Take a look at the package documentation for a description of what the functions I use do.

Can you please help me with parsing the uptime value from proc file system which is updated in every 5 second??

Do you mean this?

$ cat /proc/uptime
334147.08 464782.69

Yes… Can you please help me to write a go code to extract it which will be updated in every 5 sec

Please open a new topic about this.

Please include the Go code you have tried and describe what information you want to get from /proc/uptime.

Extract the uptime information from proc file system in linux using Go
This is the new topic for the discussion
Please review it

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