Go中函数体外的非声明语句

时间:2013-12-11 00:38:35

标签: variables scope go package global

我正在为提供JSON或XML格式数据的API构建Go库。

此API要求我每15分钟左右请求一次session_id,并在通话中使用它。例如:

foo.com/api/[my-application-id]/getuserprofilejson/[username]/[session-id]
foo.com/api/[my-application-id]/getuserprofilexml/[username]/[session-id]

在我的Go库中,我正在尝试在main() func之外创建一个变量,并打算对每个API调用的值执行ping操作。如果该值为nil或为空,请求新的会话ID,依此类推。

package apitest

import (
    "fmt"
)

test := "This is a test."

func main() {
    fmt.Println(test)
    test = "Another value"
    fmt.Println(test)

}

什么是惯用的Go方式来声明一个全局可访问的变量,而不是necesarilly一个常量?

我的test变量需要:

  • 可以从它自己的包装中的任何地方访问。
  • 可以改变

6 个答案:

答案 0 :(得分:59)

你需要

var test = "This is a test"

:=仅适用于函数,小写't'仅适用于包(未导出)。

更彻底的解释

<强> test1.go

package main

import "fmt"

// the variable takes the type of the initializer
var test = "testing"

// you could do: 
// var test string = "testing"
// but that is not idiomatic GO

// Both types of instantiation shown above are supported in
// and outside of functions and function receivers

func main() {
    // Inside a function you can declare the type and then assign the value
    var newVal string
    newVal = "Something Else"

    // just infer the type
    str := "Type can be inferred"

    // To change the value of package level variables
    fmt.Println(test)
    changeTest(newVal)
    fmt.Println(test)
    changeTest(str)
    fmt.Println(test)
}

<强> test2.go

package main

func changeTest(newTest string) {
    test = newTest
}

<强>输出

testing
Something Else
Type can be inferred

或者,对于更复杂的包初始化或设置包所需的任何状态,GO提供了一个init函数。

package main

import (
    "fmt"
)

var test map[string]int

func init() {
    test = make(map[string]int)
    test["foo"] = 0
    test["bar"] = 1
}

func main() {
    fmt.Println(test) // prints map[foo:0 bar:1]
}

在main运行之前将调用Init。

答案 1 :(得分:15)

如果您不小心使用&#34; Func &#34;或&#34; 功能&#34;或&#34; 功能&#34;而不是&#34; func &#34;你也会得到:

  

函数体外的非声明声明

发布这个是因为我最初在我的搜索中找到了解错误的地方。

答案 2 :(得分:1)

我们可以声明如下变量:

package main

import (
       "fmt"
       "time"
)

var test = "testing"
var currtime = "15:04:05"
var date = "02/01/2006"

func main() {
    t := time.Now()
    date := t.Format("02/01/2006")
    currtime := t.Format("15:04:05")

    fmt.Println(test) //Output: testing
    fmt.Println(currtime)//Output: 16:44:53
    fmt.Println(date) // Output: 29/08/2018
}

答案 3 :(得分:1)

简短的变量声明,即:=,可以在函数内使用。

例如

func main() {
    test := "this is a test"
    // or
    age := 35
}

函数外的声明,您必须根据需要使用var, func, const等关键字。(在这种情况下,我们使用的是var

>

在函数外部声明变量可使其在其程序包中访问。

package apitest

import (
    "fmt"
)
// note the value can be changed
var test string = "this is a test"

func main() {
    fmt.Println(test)
    test = "Another value"
    fmt.Println(test)

}

其他信息

如果您希望在包的内部和外部都可以访问该变量,则必须对该变量进行大写,例如

var Test string = "this is a test"

这将使它可以从任何软件包中访问。

答案 4 :(得分:0)

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

您可以在此处阅读更多信息==> https://tour.golang.org/basics/10

答案 5 :(得分:0)

当我尝试使用以下函数定义运行Go应用时遇到此错误:

std::bind

定义函数(接收器函数)的正确方法是:

(u *UserService) func GetAllUsers() (string, error) {...} //Error code