我感兴趣的是通过浏览器或CLI动态地从用户那里获取参数,以将这些参数传递给REST API调用,从而使用Go动态构造URL,最终将获取一些JSON数据.
我想知道Go中的一些技巧可以帮助我做到这一点.我认为一种理想的方法是使用地图并使用参数键和相应的值填充它,然后迭代它并将其附加到URL字符串.但是当涉及动态获取参数并填充地图时,我不太确定如何在Go中执行此操作.有人可以帮我解决Go中的一些代码片段吗?
请求示例:
http://<IP>:port?api=fetchJsonData&arg1=val1&arg2=val2&arg3=val3.....&argn=valn
Run Code Online (Sandbox Code Playgroud) 有没有办法使用像IntelliJ这样的IDE在Go中调试测试?我正在使用针对intelliJ的Go插件,看起来在提供调试配置时,调试按钮被禁用.
我试图在我尝试发送到Go服务器的请求中传递一个额外的参数 -
websocket.create_connection("ws://<ip>:port/x/y?token="qwerty")
Run Code Online (Sandbox Code Playgroud)
Go服务器实现如下 -
func main() {
err := config.Parse()
if err != nil {
glog.Error(err)
os.Exit(1)
return
}
flag.Parse()
defer glog.Flush()
router := mux.NewRouter()
http.Handle("/", httpInterceptor(router))
router.Handle("/v1/x", common.ErrorHandler(stats.GetS)).Methods("GET")
router.Handle("/v1/x/y", common.ErrorHandler(stats.GetS)).Methods("GET")
var listen = fmt.Sprintf("%s:%d", config.Config.Ip, config.Config.Port)
err = http.ListenAndServe(listen, nil)
if err != nil {
glog.Error(err)
os.Exit(1)
}
}
func httpInterceptor(router http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
startTime := time.Now()
if !auth.Auth(w, req) {
http.Error(w, "Failed authentication", 401)
return
}
router.ServeHTTP(w, req)
finishTime := time.Now() …
Run Code Online (Sandbox Code Playgroud) 来自python世界,灯具非常有用(Fixtures为可重用状态/支持逻辑定义了Python合同,主要用于单元测试).我想知道在Golang中是否有类似的支持,这可以让我用一些预定义的装置来运行我的测试,例如设置服务器,将其拆除,每次运行测试时都做一些重复的任务?有人能指出我在Golang做同样事情的一些例子吗?
我在本地更改了某些文件的权限并将其推送到 GITHUB,而我的 gitconfig 中没有 config.FileMode 标志 = false。所以 GIT 推送了文件模式更改,但我想将这些文件模式更改从 100755 恢复到 100644。我使用 MAC osx 作为我的开发环境。我不想在我的存储库上使用 git reset --hard HEAD。有什么建议 ?
我试图将一个令牌传递给这个GO库(http://godoc.org/github.com/dgrijalva/jwt-go)中为JWT定义的"Parse(令牌字符串,keyFunc Keyfunc)"GO例程 - 令牌解析/验证.
当我将令牌传递给此函数时 -
token, err := jwt.Parse(getToken, func(token *jwt.Token) (interface{}, error) {
return config.Config.Key, nil
})
Run Code Online (Sandbox Code Playgroud)
我收到一条错误,上面写着"密钥无效或类型无效".
我的config结构在config.go文件中看起来像这样 -
config struct {
Key string
}
Run Code Online (Sandbox Code Playgroud)
有什么建议可以解决这个问题?我传递的令牌是JWT令牌.
我是 CQL 的新手,并试图为我在 cassandra 中定义的表添加分页支持,如下所示 -
cqlsh:dev> create table emp4 (empid uuid , year varchar , month varchar , day varchar, primary key((year, month, day), empid));
cqlsh:dev> insert into emp4 (empid, year, month, day) values (08f823ac-4dd2-11e5-8ad6-0c4de9ac7563,'2014','03','19');
cqlsh:dev> insert into emp4 (empid, year, month, day) values (08f823ac-4dd2-11e5-8ad6-0c4de9ac7562,'2016','03','19');
cqlsh:dev> select * from emp4;
year | month | day | empid
------+-------+-----+--------------------------------------
2016 | 03 | 19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7562
2015 | 03 | 19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac756f
2014 | 03 | 19 | 08f823ac-4dd2-11e5-8ad6-0c4de9ac7563 …
Run Code Online (Sandbox Code Playgroud) 我正在使用Golang net / context包将包装在上下文对象中的ID从一项服务传递到另一项服务。我能够成功传递上下文对象,但实际上要检索特定键的值,context.Value(key)始终返回nil。我不确定为什么,但这是我到目前为止所做的:
if ctx != nil {
fmt.Println(ctx)
fmt.Println("Found UUID, forwarding it")
id, ok := ctx.Value(0).(string) // This always returns a nil and thus ok is set to false
if ok {
fmt.Println("id found %s", id)
req.headers.Set("ID", id)
}
}
Run Code Online (Sandbox Code Playgroud)
ctx是context.Context类型,在打印时得到:
context.Background.WithValue(0, "12345")
Run Code Online (Sandbox Code Playgroud)
我有兴趣从上下文中获取值“ 12345”。从Golang net / context文档(https://blog.golang.org/context)中,Value()接受interface {}类型的键并返回interface {},因此我将类型转换为。(string) 。有人可以帮忙吗?