Go:我如何创建一个全局变量来保存任何东西?

时间:2015-11-18 22:42:51

标签: go

我很好奇如何创建一个全局变量,可以在机会到来时分配给任何东西,这是我的场景:

我必须等待从发送填充结构的服务器发出的事件,并且我想将其分配给使用以下内容创建结构的变量:

func NewCS(client *thing.Thing) *structThing {

}

*structThing未导出,因此无法执行

var cs *structThing

// Event finally ready
cs = NewCS(eventData)

因为我收到*structThing未导出的错误。

那么我怎样才能使cs成为一个全局变量?

1 个答案:

答案 0 :(得分:5)

您可以将其存储在以<h2>Sign up</h2> <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> <div class="field"> <%= f.label :email %><br/> <%= f.email_field :email, autofocus: true %> </div> <div class="field"> <%= f.label :password %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> characters minimum)</em> <% end %><br/> <%= f.password_field :password, autocomplete: "off" %> </div> <div class="field"> <%= f.label :password_confirmation %><br/> <%= f.password_field :password_confirmation, autocomplete: "off" %> </div> <div class="field"> <%= f.label :roles %> <% for role in User::ROLES %> <%= check_box_tag "user[roles][]", role, @user.roles.include?(role) %> <%= h role.humanize %> <% end %> </div> <%= hidden_field_tag "user[roles][]" %> <div class="actions"> <%= f.submit "Sign up" %> </div> <% end %> <%= render "devise/shared/links" %> 键入的变量中。

interface{}

打印哪些:

package main

import "fmt"

type structThing struct {
    x int
}

func NewCS() *structThing {
    return &structThing{x: 1}
}

var cs interface{}

func main() {
    fmt.Println("cs is", cs)
    cs = NewCS()
    fmt.Println("cs is now", cs)
}

https://play.golang.org/p/ZW_6FRfDvE

相关问题