我多次听说你应该避免全局变量.
在我的例子中,我声明了一个全局myTypes变量,只是为了避免在函数调用或类似的东西中反复声明该变量.
这是怎么做的?有没有更好的办法?一种更可测试的方式?
var myTypes = map[string]string{
"type1": "tpl1",
"type2": "tpl2",
}
func AFunc(someType string) string {
fmt.Sprintf("this is your type %s", myTypes[someType])
}
func main() {
AFunc("type1")
}
Run Code Online (Sandbox Code Playgroud) 是否可以仅从gRPC服务器流式传输到某些客户端?
我相信我正在寻找的东西就像Pusher,你有一个客户端的频道,你可以发布只有有权访问该频道的客户才能看到的消息.
我正在努力的是理解我们需要采取什么步骤来做这样的事情.
考虑网络套接字我相信我们可以存储每个客户端连接,然后我们可以找到该连接并发送消息.我们怎么能做类似的事情gRPC?
我将从一个例子开始; 给定n = 1且m = 100并且列表[1,2,3]生成具有1位和2位的所有数字,依此类推但在这种情况下它们需要小于100.
输出:
- 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33
然后我们停下来,因为下一个数字将超过100,例如:
- 111, 112, 113, 121, 122, 123, 131, 132, 133, 21..,. 22..., 23..., 31, 32, 33
正如你注意到我追加1, 2, 3, 4到之前创建的一个数字,为了做到这一点,我使用了一个递归函数,它在我的列表中的每个数字的for循环中启动,它们运行直到生成的数字大于我的限制.
def x(str, finish, d, c)
return if d >= finish
[1, 2, 3, 4].each do |e|
x(str, end, d*c+e)
end
# do something if d >= str
end
Run Code Online (Sandbox Code Playgroud)
如果我需要从1开始,这可以正常工作,但如果我的起始数字要大很多,我仍然需要开始创建这个序列.
有人可以帮我一个产生相同序列的解决方案,但是从任何起点而不是1,所以如果例如起点是100和结束200那么输出将是:
111, 112, 113, 114, 121, 122, 123, …
It is possible to call a method which is in the same rake file as the task? In the code below you can see that I have the method call get_search_url which will be assigned to url.
namespace :populate do
desc "ETC"
task :db => :environment do
Report.where(link: "").each do |word|
url = get_search_url(word.name)
doc = Nokogiri::HTML(open(url))
word.update_columns(link: link)
end
end
def get_search_url(keyword)
return "URL/keyword"
end
end
Run Code Online (Sandbox Code Playgroud)