小编sha*_*ner的帖子

隐藏零值,理解为什么golang在这里失败

我无法理解如何正确地确保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)

null pointers interface go

44
推荐指数
2
解决办法
1万
查看次数

nginx条件代理传递

我试图配置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)

但这里的问题是,根始终是上手.代理不会以任何方式传递.

关于如何实现这一目标的任何想法?

提前致谢

proxy nginx

20
推荐指数
2
解决办法
3万
查看次数

nginx匹配位置中的特定单词

我在nginx $ request_body变量中匹配特定单词时遇到问题.如果正文请求中有特殊字,我想代理传递,

所以我的方法是这样的:

 location ~ \.php$ {
if ($request_body ~* (.*)) {                                        
        proxy_pass http://test.proxy;
            break;
    }

# other case...
}
Run Code Online (Sandbox Code Playgroud)

这匹配所有内容并且if语句有效,但是如果我以任何方式更改正则表达式,我都无法获得命中.

所以现在我的问题是:

我如何正确定义nginx中的正则表达式以匹配,例如"目标"?

提前致谢!

regex nginx

7
推荐指数
1
解决办法
7818
查看次数

Golang反思改进

有人知道更好的方法吗?目标是将自定义定义的字段再次从字符串转换回其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)

reflection go

3
推荐指数
1
解决办法
139
查看次数

如何获取接口{}的指针以键入检查接口实现

所以我基本上试图找到最好的方法来达到这样的目的:

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 {}类型,现在我需要调用一个接口方法,它有一个指针接收器.

我目前唯一的解决方案是动态构造一个带有反射的切片,将该元素设置为切片,然后使其成为可压缩的.

这真的是找到解决方案的唯一可能性吗? …

pointers interface go

3
推荐指数
1
解决办法
792
查看次数

标签 统计

go ×3

interface ×2

nginx ×2

pointers ×2

null ×1

proxy ×1

reflection ×1

regex ×1