dsgw8802 2014-08-23 04:50
浏览 31
已采纳

C ++静态const函数变量的Go等效项是什么?

In C++ you can write things like this:

std::string foo()
{
    const static std::vector<std::string> unchanging_data_foo_uses = {"one", "two", "three"};
    ...
}

I've always thought one important advantage of this is that this member only needs to be setup once and then on subsequent calls nothing needs to be done and it's just sitting there so that the function can do its work. Is there a nice way to do this in Go? Maybe the compiler is smart enough to see if a variable's value does not depend on the arguments then it can treat it like the above code and not do any re-evaluation?

In my specific case I am writing a Go function to convert a number into words (e.g. 42 -> "forty-two"). The following code works, but I feel dirty about work being done to setup the string arrays on every call, especially since it is recursive:

func numAsWords(n int) string {

    units := [20]string{
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen",
    }

    tens := [10]string{
        // Dummies here just to make the indexes match better
        "",
        "",

        // Actual values
        "twenty",
        "thirty",
        "forty",
        "fifty",
        "sixty",
        "seventy",                                                                                                                                
        "eighty",
        "ninety",
    }

    if n < 20 {
        return units[n]
    }

    if n < 100 {
        if n % 10 == 0 {
            return tens[n / 10]
        }

        return tens[n / 10] + "-" + units[n % 10]
    }

    if n < 1000 {
        if n % 100 == 0 {
            return units[n / 100] + " hundred"
        }

        return units[n / 100] + " hundred and " + numAsWords(n % 100)
    }

    return "one thousand"
}
  • 写回答

3条回答 默认 最新

  • dongxilan9351 2014-08-23 05:15
    关注

    You can you global variables, it's completely acceptable in go, specially for that specific case.

    And while you can't use the keyword const with arrays, you can use something like:

    //notice that since they start with lower case letters, they won't be
    // available outside this package
    var ( 
        units = [...]string{...}
        tens = [...]string{ ... }
    )
    func numAsWords(n int) string { ... }
    

    <kbd>playground</kbd>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档