2019-03-05 15:43
浏览 55


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('
    // we check if any errors have occured while reading
    // we trim the "
" from the answer to only keep the string input by the user
    answer = strings.Trim(answer, "
    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 ?

图片转代码服务由CSDN问答提供 功能建议

我正在Angular应用程序上从事翻译项目。 我已经为此创建了所有不同的键。 我现在尝试使用Go编程语言在我的翻译中添加一些功能,以便在以后快速工作。

我尝试用Go编程语言对函数进行编码,以便在Windows上读取输入用户 命令行。 我需要阅读此输入文件,以了解内部是否缺少键。 此输入用户必须是JSON文件。 我对此功能有问题,在 functions.Check(err)处被阻止,为了调试我的功能,我用 fmt.Printf(显示的变量)显示了另一个变量。 我在主函数中将此函数称为 readInput()


func readInput()字符串{
读取器:= bufio  .NewReader(os.Stdin)
 err:= reader.ReadString('
 //我们检查在读取函数时是否发生任何错误。  (err)
 answer = strings.Trim(answer,“ 

在我的主要功能中,我针对创建的特定命令调用 readInput()。 此命令行对于更新JSON文件和自动添加丢失的密钥非常有用。

我的 func main 是:

  func main(){
,如果os.Args [1] ==“ update-json-from-json” {
 fmt.Printf(“请输入将要保存的json文件的名称 用于
 jsonFile:= readInput()
  // //读取用户的输入
语言环境:= readInput()

我可以为您提供用于此代码的命令行运行mis-t.go update-json-from-json


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongou5100 2019-03-05 16:06

    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)

    to load the contents into the empty interface, and then use the go reflection library ( 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).

    解决 无用
    打赏 举报

相关推荐 更多相似问题