Home  >  Article  >  Backend Development  >  Go Kafka - configuration values

Go Kafka - configuration values

WBOY
WBOYforward
2024-02-06 08:21:081149browse

Go Kafka - 配置值

Question content

I am using go-kafka (https://pkg.go.dev/github.com/confluenceinc/confluence-kafka- go/kafka#configmap.get), and I'm having trouble using the get method of configmap.

The documentation says that get requires a key and a defval (of type configvalue), but I don't know how to create a configvalue to pass in.

This is what I tried:

configfile := os.args[1]
    conf := readconfig(configfile)
    
    x := conf["sasl.password"]

    fmt.printf(conf.get("sasl.password", x))
configFile := os.Args[1]
    conf := ReadConfig(configFile)
    
    x := kafka.ConfigValue("Test")

    fmt.Printf(conf.Get("sasl.password", x))

But every time I get the same error:

Cannot use 'conf.get("sasl.password", x)' (type (configvalue, error)) as type (string, any) or string

Where did I go wrong?


Correct Answer


As far as your use of configmap.get is concerned, you are not doing anything wrong per se.

The error you are seeing is caused by your use of fmt.printf.

The immediate error is because get() returns a value and an error ((configvalue, error)) which you are trying to pass directly to printf() , which takes a string containing a format specifier and a list of variables to be replaced with any values ​​from these format specifications.

You first need to get the configmap value and test for errors:

   v, err := conf.get("sasl.password", x)
   if err != nil {
      // handle/return the error
   }

Then, if err is nil, you can use the %s specification to output the value to printf and pass in the return value:

   if err == nil {
      fmt.Printf("%s", v)
   }

The above is the detailed content of Go Kafka - configuration values. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete