如何将我的全局变量移动到golang中的其他包?
喜欢
package main
import "myapp/controllers"
var something string
func main(){
something = "some text"
}
答案 0 :(得分:0)
就像您希望在包controllers
中拥有一个全局可访问的变量?:
package controllers
var Something string
func init(){
Something = "some text"
}
然后你可以做
package main
import (
"myapp/controllers"
"fmt"
)
func main(){
fmt.Println(controllers.Something) //some text
}