0

I'm working on a translation project on my Angular app. I already create all the different keys for that. I try now to use Go Programming Language to add some functionalities in my translation, to work quickly after.

I try to code a function in Go Programming Language in order to read an input user on the command line. I need to read this input file in order to know if there is missing key inside. This input user must be a JSON file. I have a problem with this function, is blocked at functions.Check(err), in order to debug my function I displayed the different variable with fmt.Printf(variable to display). I call this function readInput() in my main function.

The readInput() function is the following :

    // this function is used to read the user's input on the command line
func readInput() string {
    // we create a reader
    reader := bufio.NewReader(os.Stdin)
    // we read the user's input
    answer, err := reader.ReadString('\n')
    // we check if any errors have occured while reading
    functions.Check(err)
    // we trim the "\n" from the answer to only keep the string input by the user
    answer = strings.Trim(answer, "\n")
    return answer
}

In my main function I call readInput() for a specific command I created. This command line is usefull to update a JSON file and add a missing key automatically.

My func main is :

      func main() { 
        if os.Args[1] == "update-json-from-json" {

    fmt.Printf("please enter the name of the json file that will be used to 
    update the json file:") 
    jsonFile := readInput()

    fmt.Printf("please enter the ISO code of the locale for which you want to update the json file: ")
            // we read the user's input
            locale := readInput()
            // we launch the script
            scripts.AddMissingKeysToJsonFromJson(jsonFile, locale)
        }

I can give you the command line I use for this code go run mis-t.go update-json-from-json

Do you what I'm missing in my code please ?

4
  • 1
    go has the json package wich deals with json Commented Mar 5, 2019 at 15:45
  • @Pizzalord can you be more precise please ? Commented Mar 5, 2019 at 15:53
  • just google golang json the package and its documentation will be the first result. i do not know how you did not find it Commented Mar 5, 2019 at 15:55
  • I found this doc, but If I asked the question here with details, it's I need help to understand what I missed in this information @Pizzalord Commented Mar 5, 2019 at 15:59

1 Answer 1

1

Presuming that the file contains dynamic and unknown keys and values, and you cannot model them in your application. Then you can do something like:


func main() {
    if os.Args[1] == "update-json-from-json" {
        ...
        jsonFile := readInput()

        var jsonKeys interface{}
        err := json.Unmarshal(jsonFile, &jsonKeys)
        functions.Check(err)


        ...
    }
}

to load the contents into the empty interface, and then use the go reflection library (https://golang.org/pkg/reflect/) to iterate over the fields, find their names and values and update them according to your needs.

The alternative is to Unmarshal into a map[string]string, but that won't cope very well with nested JSON, whereas this might (but I haven't tested it).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.