可以说有方法:
def in_block
x = "This variables outer block"
3.times do |i|
x = i
puts "x inside block - #{x}"
end
puts x
end
in_block
# >> x inside block - 0
# >> x inside block - 1
# >> x inside block - 2
# >> 2
Run Code Online (Sandbox Code Playgroud)
我如何保护我的x变量?
使用以下egrep命令,我从/ etc/hosts文件中获取主机,如下例所示
egrep "Solaris|linux|unix|vms|win|proxy|terminal|unixware" /etc/hosts
192.9.200.12 Solaris1
192.9.200.13 Solaris2
192.9.200.14 Solaris3
192.9.200.15 Solaris4
192.9.200.16 Solaris5
192.9.200.17 linux1
192.9.200.18 linux2
192.9.200.19 linux3
192.9.200.20 linux4
192.9.200.21 linux5
Run Code Online (Sandbox Code Playgroud)
我想要的是添加awk或sed或perl one liner命令,它将仅打印两个第一个匹配主机,如下所示
egrep "Solaris|linux|unix|vms|win|proxy|terminal|unixware" | ...... /etc/hosts
192.9.200.12 Solaris1
192.9.200.13 Solaris2
192.9.200.17 linux1
192.9.200.18 linux2
Run Code Online (Sandbox Code Playgroud) 我希望构建一个ruby正则表达式来匹配模式的多次出现并将它们返回到数组中.模式很简单:[[.+]].也就是说,两个左括号,一个或多个字符,后跟两个右括号.
这就是我所做的:
str = "Some random text[[lead:first_name]] and more stuff [[client:last_name]]"
str.match(/\[\[(.+)\]\]/).captures
Run Code Online (Sandbox Code Playgroud)
上面的正则表达式不起作用,因为它返回:
["lead:first_name]] and another [[client:last_name"]
Run Code Online (Sandbox Code Playgroud)
当我想要的是这个:
["lead:first_name", "client:last_name"]
Run Code Online (Sandbox Code Playgroud)
我想如果我使用非捕获组,肯定它应该解决问题:
str.match(/(?:\[\[(.+)\]\])+/).captures
Run Code Online (Sandbox Code Playgroud)
但非捕获组返回相同的错误输出.关于如何解决我的问题的任何想法?
我正在尝试在执行控制器操作 3 分钟后运行作业。
我用 DelayedJob 试过这个:
# in my controller
def index
@books = Book.all
Delayed::Job.enqueue(ReminderJob.new(params[:user_id]))
end
Run Code Online (Sandbox Code Playgroud)
在ReminderJob.rb文件中:
class ReminderJob < Struct.new(:user_id)
def perform
p "run reminder job"
# do something
end
handle_asynchronously :perform, :run_at => Proc.new { 3.minutes.from_now }
end
Run Code Online (Sandbox Code Playgroud)
但是,在访问索引页面时,我在日志中没有看到任何内容,3 分钟后什么也没有发生。
我做错了什么 ?是否有另一种推荐的方法可以在不使用的情况下“在 X 分钟内”运行任务sleep?
我用 保存一个数字params[:number].gsub(/\D/,''),但我不想去掉加号:+
例如,如果用户保存数字+1 (516) 949-9508,则将其另存为,15169499508但我们如何保留+as +15169499508?
有人可以解释发生了什么吗?我可能会缺少一些东西吗?我是新手。我正在制作一个简单的CRUD应用程序,它可以正常编译,但是当我启动服务器时,它给了我一个运行时错误。
2017/10/08 11:11:59 http: multiple response.WriteHeader calls
2017/10/08 11:11:59 http: panic serving [::1]:46828: runtime error: invalid memory address or nil pointer dereference
goroutine 19 [running]:
net/http.(*conn).serve.func1(0xc42008ce60)
/home/vikram/go/src/net/http/server.go:1697 +0xd0
panic(0x740160, 0x97cc10)
/home/vikram/go/src/runtime/panic.go:491 +0x283
html/template.(*Template).escape(0x0, 0x0, 0x0)
/home/vikram/go/src/html/template/template.go:95 +0x38
html/template.(*Template).Execute(0x0, 0x94d920, 0xc420186000, 0x719e00, 0x9ab500, 0xc4201820c0, 0x0)
/home/vikram/go/src/html/template/template.go:119 +0x2f
main.indexHandler(0x9519e0, 0xc420186000, 0xc420160100)
/home/vikram/Projects/golang/src/github.com/vikramdurai/blog-app-go/main.go:188 +0x185
net/http.HandlerFunc.ServeHTTP(0x7b8da8, 0x9519e0, 0xc420186000, 0xc420160100)
/home/vikram/go/src/net/http/server.go:1918 +0x44
net/http.(*ServeMux).ServeHTTP(0x98ad20, 0x9519e0, 0xc420186000, 0xc420160100)
/home/vikram/go/src/net/http/server.go:2254 +0x130
net/http.serverHandler.ServeHTTP(0xc420087110, 0x9519e0, 0xc420186000, 0xc420160100)
/home/vikram/go/src/net/http/server.go:2619 +0xb4
net/http.(*conn).serve(0xc42008ce60, 0x9520a0, 0xc42007c280)
/home/vikram/go/src/net/http/server.go:1801 +0x71d
created by net/http.(*Server).Serve
/home/vikram/go/src/net/http/server.go:2720 +0x288
Run Code Online (Sandbox Code Playgroud)
我的 …
我多年来一直在使用 Node/Python 进行开发,假期里我一直在努力扩展我对 Go 的了解。我有一个我一直在努力学习它的宠物项目。
当我一直在阅读gin-gonic 文档时,我发现我无法理解的语法。
func main() {
router := gin.Default()
// Simple group: v1
v1 := router.Group("/v1")
{
v1.POST("/login", loginEndpoint)
v1.POST("/submit", submitEndpoint)
v1.POST("/read", readEndpoint)
}
// Simple group: v2
v2 := router.Group("/v2")
{
v2.POST("/login", loginEndpoint)
v2.POST("/submit", submitEndpoint)
v2.POST("/read", readEndpoint)
}
router.Run(":8080")
}
Run Code Online (Sandbox Code Playgroud)
基本上,该router.Group()方法看起来像一个结构体,但我不太确定这里的机制是什么......它没有用括号括起来。这个对象是如何被方法传入/处理的Group?
在对其数据执行验证后,我试图用它的数据恢复上下文。我需要数据在下一个函数中根据需要继续移动。
我是 golang 的新手,下面的代码是我所能做的。非常感谢任何帮助和更好的方法。
提前致谢。
验证中间件
func SignupValidator(c *gin.Context) {
// Read the Body content
// var bodyBytes []byte
// if c.Request.Body != nil {
// bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
// }
var user entity.User
if err := c.ShouldBindJSON(&user); err != nil {
validate := validator.New()
if err := validate.Struct(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
c.Abort()
return
}
// c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
}
// Read the Body content
var bodyBytes []byte
if c.Request.Body != nil { …Run Code Online (Sandbox Code Playgroud) 我\xe2\x80\x99m 测试我的一个http 处理程序。\n像这样的curl 可以工作:curl localhost:8080/v1/parts/2bb834c9-8e17-4e2c-80b9-a20b80732899。\n但是相应的测试失败,因为处理程序无法从URL 中提取id。
请看一下这个测试文件。我不\xe2\x80\x99不明白为什么我的curl命令有效但第17行的newRequest失败。
\n 1 package part\n 2\n 3 import (\n 4 "net/http"\n 5 "net/http/httptest"\n 6 "testing"\n 7\n 8 "github.com/labstack/echo/v4"\n 9 "github.com/stretchr/testify/assert"\n 10 )\n 11\n 12 var handler *Handler\n 13\n 14 func TestGetPart(t *testing.T) {\n 15 e := echo.New()\n 16\n 17 req := httptest.NewRequest("GET", "/v1/parts/2bb834c9-8e17-4e2c-80b9-a20b80732899", nil)\n 18 rec := httptest.NewRecorder()\n 19 c := e.NewContext(req, rec)\n 20\n 21 if assert.NoError(t, handler.handleGetPart(c)) {\n 22 assert.Equal(t, http.StatusOK, rec.Code)\n 23 }\n 24 }\n\nRun Code Online (Sandbox Code Playgroud)\n去测试 -v ./internal/handler/part …
我使用 golang jwt 中间件。
e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte(os.Getenv("Signing_Key")),
TokenLookup: "header:x-auth-token",
}))
Run Code Online (Sandbox Code Playgroud)
它等待所有功能的令牌,但我不想使用这个中间件进行登录功能。如何防止这种情况发生?