我无法理解如何正确地确保nil在这种情况下不存在某些事情:
package main
type shower interface {
getWater() []shower
}
type display struct {
SubDisplay *display
}
func (d display) getWater() []shower {
return []shower{display{}, d.SubDisplay}
}
func main() {
// SubDisplay will be initialized with null
s := display{}
// water := []shower{nil}
water := s.getWater()
for _, x := range water {
if x == nil {
panic("everything ok, nil found")
}
//first iteration display{} is not nil and will
//therefore work, on the second iteration …Run Code Online (Sandbox Code Playgroud) 我试图配置nginx代理将请求传递给另一个服务器,只有当$ request_body变量匹配特定的正则表达式时.
我现在的问题是,我不知道如何准确配置此行为.
我现在正处于这个问题:
server {
listen 80 default;
server_name test.local;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
if ($request_body ~* ^(.*)\.test) {
proxy_pass http://www.google.de;
}
root /srv/http;
}
}
Run Code Online (Sandbox Code Playgroud)
但这里的问题是,根始终是上手.代理不会以任何方式传递.
关于如何实现这一目标的任何想法?
提前致谢
我在nginx $ request_body变量中匹配特定单词时遇到问题.如果正文请求中有特殊字,我想代理传递,
所以我的方法是这样的:
location ~ \.php$ {
if ($request_body ~* (.*)) {
proxy_pass http://test.proxy;
break;
}
# other case...
}
Run Code Online (Sandbox Code Playgroud)
这匹配所有内容并且if语句有效,但是如果我以任何方式更改正则表达式,我都无法获得命中.
所以现在我的问题是:
我如何正确定义nginx中的正则表达式以匹配,例如"目标"?
提前致谢!
有人知道更好的方法吗?目标是将自定义定义的字段再次从字符串转换回其int类型.
switch val.Kind() {
case reflect.Int:
intID, err := strconv.ParseInt(id, 10, 0)
if err != nil {
return err
}
val.Set(reflect.ValueOf(int(intID)))
case reflect.Int8:
intID, err := strconv.ParseInt(id, 10, 8)
if err != nil {
return err
}
val.Set(reflect.ValueOf(int8(intID)))
case reflect.Int16:
intID, err := strconv.ParseInt(id, 10, 16)
if err != nil {
return err
}
val.Set(reflect.ValueOf(int16(intID)))
case reflect.Int32:
intID, err := strconv.ParseInt(id, 10, 32)
if err != nil {
return err
}
val.Set(reflect.ValueOf(int32(intID)))
case reflect.Int64:
intID, err := strconv.ParseInt(id, 10, 64) …Run Code Online (Sandbox Code Playgroud) 所以我基本上试图找到最好的方法来达到这样的目的:
package main
import "fmt"
type SomeStruct struct {
}
type SomeInterface interface {
SomeMethodWhichNeedsAPointerReceiver() string
}
func (s *SomeStruct) SomeMethodWhichNeedsAPointerReceiver() string {
return "Well, believe me, I wrote something"
}
func Run(s interface{}) {
// how can I cast s to a pointer here?
casted, ok := (s).(SomeInterface)
if ok {
fmt.Println("Awesome: " + casted.SomeMethodWhichNeedsAPointerReceiver())
return
}
fmt.Println("Fail :(")
}
func SomeThirdPartyMethod() interface{} {
return SomeStruct{}
}
func main() {
x := SomeThirdPartyMethod()
Run(x)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在Run方法的类型转换中.我基本上只知道它是interface {}类型,现在我需要调用一个接口方法,它有一个指针接收器.
我目前唯一的解决方案是动态构造一个带有反射的切片,将该元素设置为切片,然后使其成为可压缩的.
这真的是找到解决方案的唯一可能性吗? …