路由器状态运行

huangapple go评论143阅读模式
英文:

Router condition run

问题

我有一个API,我想用不同的条件来运行这个API,就像这样,这是我的配置:

config := api.Config{
    Version:          VERSION,
    Host:             Setting.Host,
    Key:              Setting.Key,
    Https:            Setting.Https,
    Enable:           Setting.Enable,
}

如果在配置文件中的Enable字段为true,我想要使用额外的值来运行路由器:

if config.Enable {
    test_f := func() (func(), error) {
        return df, nil
    }
    router := api.MakeRouter(&config, test_f)
}

当Enable字段为false时:

else {
    router := api.MakeRouter(&config, nil)
}

但是我的问题是,我有另一个条件。在这里,我得到了一个错误,错误信息是undefined: router

if config.Https {
    go router.RunTLS(config.Host, config.Key)
} else {
    go router.Run(config.Host)
}

我该如何解决这个问题?

英文:

I have api I want to run this api with different conditions it is like
this is my config

	config := api.Config{
		Version:          VERSION,
		Host:             Setting.Host,
        Key:              Setting.Key,
		Https:            Setting.Https,
		Enable:           Setting.Enable,
	}

if in config yaml Enable is true I want to run the router with extra value

	if config.Enable {
		test_f := func() (func(), error) {
			return df, nil
		}
		router := api.MakeRouter(&config, test_f)
	}

when enable is false

    else {
        router := api.MakeRouter(&config, nil)

    }

but my problem is that I have another condition. in here I get an error which is undefined: router

	if config.Https {
		go router.RunTLS(config.Host, config.Key)
	} else {
		go router.Run(config.Host)
	}
```
how can I solve this

</details>


# 答案1
**得分**: 1

你正在在一个代码块内定义变量`router`,这意味着它的作用域仅限于该代码块内部,在该代码块外部是不存在的。你需要在代码块外部定义它,并在内部进行赋值:

```go
var router api.WhateverTypeRouterIs
if config.Enable {
    test_f := func() (func(), error) {
        return df, nil
    }
    router = api.MakeRouter(&config, test_f)
} else {
    router = api.MakeRouter(&config, nil)
}
```

或者,另一种方法是,由于只有一个值不同,在`if`代码块中处理该值,并在外部创建路由器:

```go
var test_f func() (func(), error)
if config.Enable {
    test_f = func() (func(), error) {
        return df, nil
    }
}
router := api.MakeRouter(&config, test_f)
```

一般来说,我更倾向于使用后一种方法,因为它更清晰明了,但如果在两种情况下有更多的变化,那么第一种方法可能更合适。

标识符的作用域在规范中有详细说明:https://go.dev/ref/spec#Declarations_and_scope

<details>
<summary>英文:</summary>

You&#39;re defining the variable `router` inside a block, which means it is scoped to that block; it doesn&#39;t exist outside that block. You need to define it outside the block, and assign to it inside:


    var router api.WhateverTypeRouterIs
    if config.Enable {
        test_f := func() (func(), error) {
            return df, nil
        }
        router = api.MakeRouter(&amp;config, test_f)
    } else {
        router = api.MakeRouter(&amp;config, nil)
    }

Or, alternatively, since only one value is different, deal with that value only in the `if` block, and create the router outside:

    var test_f func() (func(), error)
    if config.Enable {
        test_f = func() (func(), error) {
            return df, nil
        }
    }
    router := api.MakeRouter(&amp;config, test_f)

The latter would be my preference in general because it&#39;s more clear what&#39;s happening, but if more things are likely to change between the two cases, that might be an argument for the first approach.

Identifier scope is detailed in the spec: https://go.dev/ref/spec#Declarations_and_scope

</details>



huangapple
  • 本文由 发表于 2022年1月5日 22:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/70594340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定