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

Go Kafka - configuration values

WBOY
Release: 2024-02-06 08:21:08
forward
1230 people have browsed it

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))
Copy after login
configFile := os.Args[1]
    conf := ReadConfig(configFile)
    
    x := kafka.ConfigValue("Test")

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

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
   }
Copy after login

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)
   }
Copy after login

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

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template