简体   繁体   English

在golang中使用条件块之外的变量

[英]Use variable out of conditional block in golang

func CheckKafkaReadPartitions(kafkabroker string, topic string, conf config.Config) bool {
    var conn *kafka.Conn

    if conf.TlsEnabled {
        d := &kafka.Dialer{
            TLS: &tls.Config{},
        }
        conn, err := d.Dial("tcp", kafkabroker)
        log.Info("conn is: ", conn)
        log.Info("Using TLS connection")
        if err != nil {
            log.WithError(err).Warn("Kafka broker connection error")
            return false
        }
        defer conn.Close()
    } else {
        conn, err := kafka.Dial("tcp", kafkabroker)
        log.Info("conn is: ", conn)
        log.Info("Using Plaintext connection")
        if err != nil {
            log.WithError(err).Warn("Kafka broker connection error")
            return false
        }
        defer conn.Close()
    }
    log.Info("conn is: ", conn)
    log.Info("Reading Partitions")
    partitions, err := conn.ReadPartitions()
 // SOME OTHER WORK
}

I noticed that when calling ReadPartitions() method, conn is empty even after affecting values to it either in conn, err:= kafka.Dial("tcp", kafkabroker) or conn, err:= d.Dial("tcp", kafkabroker) What am I missing?我注意到在调用 ReadPartitions() 方法时,即使在conn, err:= kafka.Dial("tcp", kafkabroker)conn, err:= d.Dial("tcp", kafkabroker)我错过了什么? Is there any way I could take conn var out of that if/else block without emptying its content?有什么办法可以在不清空其内容的情况下从 if/else 块中取出 conn var 吗?

So basically what happens here is a variable shadowing .所以基本上这里发生的是一个variable shadowing

Go has variable scopes, you can have a variable in a global scope by defining it outside of a function. Go 具有变量范围,您可以通过在 function 之外定义变量来在全局 scope 中拥有一个变量。 Then you'll be able to use this variable anywhere in the same package (or if it is exported anywhere in your code).然后你就可以在同一个 package 的任何地方使用这个变量(或者如果它被导出到你的代码中的任何地方)。

Then you have the variables that are defined in a block of code.然后你就有了在代码块中定义的变量。 Similar to var conn *kafka.Conn you can access this variable from everywhere in the block it was defined (and all the sub-blocks).var conn *kafka.Conn类似,您可以从定义它的块(以及所有子块)中的任何位置访问此变量。

Think the blocks as the code that is enclosed by the curly brackets {} This means that the if/else are separate blocks under the func block.将块视为由大括号{}括起来的代码,这意味着if/elsefunc块下的单独块。

Now what you need to understand is the difference between the = and :=现在您需要了解的是=:=之间的区别

= is used to assign a value to a variable while := is a shorthand that is used to declare and assign a variable. =用于为变量赋值,而:=是用于声明和分配变量的简写。

So by using conn, err:= d.Dial("tcp", kafkabroker) code what you essentially do is declare new variables in the if block and assign the values to them from the returned values of the d.Dial func cal.因此,通过使用conn, err:= d.Dial("tcp", kafkabroker)代码,您实际上所做的就是在if块中声明新变量,并从d.Dial func cal 的返回值中为它们分配值。

There are some cases that you may want to do that.在某些情况下,您可能想要这样做。 The most common case is when you have a for loop that launches goroutines that uses a variable from an outer block.最常见的情况是当你有一个 for 循环来启动使用外部块中的变量的 goroutines 时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM