Go With Json file

Hi I am New To Go Code Could You Please Help Me To Get This Done ?

1)Consider the json file containing employee id,name and salary and write a program to calculate the total salary paid to the employee.(display on webpage)

2)Consider Above Scenario and. Write a program to display the employee details containing the salary more than 5000(on webpage)

How exactly does the JSON look like and do you have some (not) working code that shows what you have achieved so far and what you have trouble with?

1 Like
package main
 
import (
    "encoding/json"
    "fmt"
    "io"
    "log"
    "strings"
    "net/http"
)
 
func Handle(w http.ResponseWriter,r *http.Request) {
    const jsonStream=  `
				        {"id":1, "Name": "John",  "Salary": 54552}        
				        {"id":2, "Name": "Mark",  "Salary": 98545}
				        {"id":3, "Name": "Sandy", "Salary": 34534}
				        {"id":4, "Name": "Holard","Salary": 34534}
				        `
    type Employee struct {
        id int
        Name string
        Salary int
    }

    dec := json.NewDecoder(strings.NewReader(jsonStream))
    for {
        var emp Employee
        if err := dec.Decode(&emp); err == io.EOF {
            break
        } else if err != nil {
            log.Fatal(err)
        }
        fmt.Fprintf(w,"%d: %s: %d\n", emp.id,emp.Name, emp.Salary)
    }
}

func main(){
	http.HandleFunc("/",Handle)
	http.ListenAndServe(":8080",nil)
}

How To Add Only Salary field of all employees and return this in webpage ?

You only write the details of each emp to the response.

Have you tried to implement the logic to calculate the sum and only write the high salaries?

Iam Not Getting that only could you plz help

It looks like somebody (your teacher?) gave you an assignment to solve. It also looks like you have been given some code that implements JSON parsing and writing a HTTP response.

Now it is your task to implement the rest, we are not going to do it for you. What have you tried to

  • calculate the total salary paid to the employee,
  • display the total salary by writing it to w,
  • find the employee details containing the salary more than 5000
  • display them by writing to w?
2 Likes

i Just Coded Above But I am unable to implement that logic with json so i posted here.i am learning go these are just to test my codingand i thought you guys will help not to judge the situation right.

This is quite easy. You already have emp and emp.Salary. Now define an int outsinde the for loop to sum the salaries. Inside the loop add the salary to this sum. Outside the loop write the sum to w. That would be enough to implement the first part of your assignment. Try it!

Could You Just Implement It ?

Sure. But I already know how this works. You learn by trying yourself, making mistakes, fixing errors. You do not learn by watching me solving your problem.

1 Like

i didnt get it so i asked here to solve this

Then lats start with simple babysteps @Mani_V.

Please write the implementation of the sum() function:

package main

// sum sums the elements of a slice of int
func sum(s []int) int {
  // implement me
}

func main() {
  fmt.Println(sum([1,2,3,4]))
}

The output of this should be 10.

1 Like

what i asked and what you guys are saying ?

I don’t quite understand you. Do you really expect us to write your code for you? Even in a case that smells like homework from three miles away?

You asked us to do your work.

We said, you won’t learn from us doing your code.

Therefore we try to give you some hints to get the code written yourself.

Basically, what I asked you to write, is the first step top properly summing up your JSONs salaries. Its just changing the type of s and how you access it.

When you have that sum ready, converting it to a string and writing and back to a stream becomes trivial.

Filtering a slice for step 2 is about as trivial as summing it up in the first place after one has understood how to properly work with slices.

And again, converting it to a string and writing this to a stream is trivial.

If though you have special design considerations for your web page, you need to ask in another forum that is more into web design.

Programm is not about just solving problems. It is about breaking bigger problems into smaller ones and solving them piece wise.

The problems you have to solve in order:

  1. Sum a slice of numbers
  2. Sum a slice of structs with a given field you want the sum of
  3. Convert a number to a string
  4. Write a string to a Writer
  5. Filter a slice of numbers
  6. Filter a slice of structs
  7. Convert a slice of structs to a string
  8. See 4.

If though you still insist on other people writing yyour code, I think #jobs is the place to go, but you will probably not learn much that way.

1 Like

Okay Good.
Thanks !

Just for your information, Google found an answer to summing up the slice of numbers in less time that I’d need to actually copy and paste that solution into a post and format it.

Of course you need to know what to search for, but I’ve given away the necessary buzzwords not only in this post but through the whole thread.

Remember we are all doing this in our spare and free time, and we have been were you are as well, and believe me, I were not where I’m now, getting paid a full-time salary for a halftime job, if I only copy pasted solutions without ever thinking about it.

3 Likes

Solved !
As i was Unmarshalled the json and i had done operations on struct.

Thanks ! Can You Plz Tell some hints for the 2nd one too

What is it you are stuck with?

In general I already gave 4 hints for your second task.

Are you stuck with one of these? Are you trying it differently?

But roughly it’s building a new slice and only appending those items that fullfil a certain criteria.

After that you just need to output your result.