小编Anu*_*dha的帖子

在golang concat多片

我正在尝试合并多个切片,如下所示,

package routes

import (
    "net/http"
)

type Route struct {
    Name        string
    Method      string
    Pattern     string
    Secured     bool
    HandlerFunc http.HandlerFunc
}

type Routes []Route

var ApplicationRoutes Routes

func init() {
    ApplicationRoutes = append(
        WifiUserRoutes,
        WifiUsageRoutes,
        WifiLocationRoutes,
        DashboardUserRoutes,
        DashoardAppRoutes,
        RadiusRoutes,
        AuthenticationRoutes...
    )
}
Run Code Online (Sandbox Code Playgroud)

但是内置的append()能够追加两个切片,因此它会在编译时抛出太多参数来追加.是否有替代功能来完成任务?或者有更好的方法来合并切片?

go

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

模板内的算术运算

我正在尝试在木偶模板中的参数中添加一个数字,如下所示

"https://localhost:<%= 9443 + @offset %>/service/" 
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误.

细节:字符串无法强制进入Fixnum

'offset'是一个数值.是否有可能在木偶中进行这种算术运算?

erb puppet

6
推荐指数
1
解决办法
3529
查看次数

CGO 在 MacOS 上忽略 LDFLAGS -L 选项

我正在尝试在 MacOS 机器上编译以下代码

// #cgo darwin LDFLAGS: -L${SRCDIR}/build/darwin -lprocessing_lib
// #cgo linux LDFLAGS: -L${SRCDIR}/build/linux -lprocessing_lib
// #include "Processing-bridge.h"
// #include <stdlib.h>
import "C"
import "unsafe"

type ProcessorWrapper struct {
    ptr unsafe.Pointer
}

func init() {
    pr.ptr = C.NewProcessor()
}

func GetDefault() (id int, name string) {
    var default = C.GetDefault(pr.ptr)
    id = int(default.materialId)
    name = C.GoString(default.name)
    return
}
Run Code Online (Sandbox Code Playgroud)

我在 LDFLAGS 中定义的正确位置有 libprocessing_lib.so 文件。但是编译器仍然抛出一个错误说

dyld: Library not loaded: build/lib_processing_lib.so
  Referenced from: /Users/ag/workspace/src/github.com/apremalal/pq-processor/./pq-processor
  Reason: image not found
Run Code Online (Sandbox Code Playgroud)

一旦我将 build/lib_processing_lib.so 复制到项目的根目录,它就可以工作了。

似乎 LDFLAGS …

gcc go cgo

6
推荐指数
1
解决办法
1768
查看次数

如何从容器本身获取容器 ID

我试图在启动时获取正在运行的容器的容器 ID。这是为了在服务运行状况检查 api 中使用该信息。

我在一组容器前面放置了一个负载均衡器,并通过https://service-n.api.com/health. 想法是通过 api 响应返回容器信息。

我正在使用 docker-compose 来启动 docker 容器,如果有一种方法可以将容器 id 作为环境变量传递给容器,那就太好了,如下所示。

version: '2'
services:
  web:
    image: my.registry.com/pq-api:1.0.0
    container_name: my-container
    ports:
      - 80:80
      - 443:443
    network_mode: bridge
    environment:
      CONTAINER_ID: "{{.ID}}"
Run Code Online (Sandbox Code Playgroud)

docker docker-compose

5
推荐指数
1
解决办法
4583
查看次数

Git rebase导致pull请求在自动合并中失败

我正在我的fork中使用分支B上的一个功能,我使用rebase策略将主分支更改为B.

今天我面临的情况是,在将主人变为B之后(上游没有对B进行任何更改),我不得不强行将这些更改推送到我的分支(这里正常推送失败了).

然后我创建了一个从我的B到Master repos B的拉取请求,其他开发人员也在这里工作,但是创建的拉取请求无法自动合并.

处理这类情况的最佳方法是什么?

git github

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

omitempty 不会省略 JSON 中的接口 nil 值

我试图省略 nil 接口值

package main

import (
    "fmt"
    "encoding/json"
)

type MyStruct struct{
 Val interface{} `json:"val,omitempty"`
}

func main() {
    var s []string
    s = nil
    m := MyStruct{
    Val : s,
    }

    b, _:=  json.Marshal(m)
    fmt.Println(string(b))
}
Run Code Online (Sandbox Code Playgroud)

这是操场链接https://play.golang.org/p/cAE1IrSPgm 这输出

{"val":null}
Run Code Online (Sandbox Code Playgroud)

为什么不将其视为空值?有没有办法从 json.json 中省略这些 nil 值。

json go

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

目录中的代码需要导入

我有一个从存储库分叉的包myproject。里面的项目,我想使用一些功能从sha3包装,但是,我需要首先添加go文件到sha3其中包含一些额外的功能包。我想sha3在我的项目中包含这个自定义包。我将sha3目录复制并粘贴到myproject目录中,在我的go代码中,我将sha3包导入为: import . "github.com/myproject/sha3". 现在,当我尝试构建myproject包时,我得到: code in directory /src/github.com/myproject/sha3 expects import "golang.org/x/crypto/sha3". 我无法理解问题是什么。我检查了目录中的所有go文件sha3,它们都不需要任何导入!

go gopath

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

如何在golang中将json对象数组插入mongodb

我的json看起来如下,

[
  {
    "key1": 1,
    "key2": "val2"
  },
  {
    "key1": 2,
    "key2": "val2"
  }
]
Run Code Online (Sandbox Code Playgroud)

这个json是字符串格式,我希望json数组中的对象作为单独的记录插入到mongodb中.我提到了https://labix.org/mgo,但未能在上述用例中找到足够的示例.感谢您寻找解决方案的想法.

go mongodb mgo

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

为什么Golang结构数组无法分配给接口数组

我正在尝试实现以下目标。

package main

import (
    "fmt"
)

type MyStruct struct {
    Value int
}

func main() {
    x := []MyStruct{
        MyStruct{
            Value : 5,
        },
        MyStruct{
            Value : 6,
        },
    }
    var y []interface{}
    y = x // This throws a compile time error

    _,_ = x,y
}
Run Code Online (Sandbox Code Playgroud)

这给出了编译时错误:

sample.go:21: cannot use x (type []MyStruct) as type []interface {} in assignment
Run Code Online (Sandbox Code Playgroud)

为什么这不可能呢?如果没有,则没有其他方法可以在Golang中保存通用对象数组吗?

go

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

如何避免 RDS 实例和安全组的模块中出现循环错误

我正在编写一个 terraform 模块,该模块提供 RDS 数据库实例及其用于控制​​入站连接的关联安全组。我遇到的问题是安全组资源需要数据库端口作为参数,而数据库实例资源需要安全组 ID 作为参数。因此出现循环错误。

resource "aws_security_group" "this" {
  name = "${local.name}-inbound"
  description = "Allow inbound traffic from customer instances and management"
  vpc_id = "${var.vpc_id}"
  ingress {
    from_port = "${aws_db_instance.this.port}"
    to_port   = "${aws_db_instance.this.port}"
    protocol  = 6
    security_groups = ["${var.ingress_sg_ids}"]
  }
}

resource "aws_db_instance" "this" {
  allocated_storage       = "${var.storage_size}"
  storage_type            = "${var.storage_type}"
  engine                  = "${var.db_engine}"
  engine_version          = "${var.db_engine_version}"
  instance_class          = "${var.instance_type}"
  identifier_prefix       = "${local.name}-"
  name                    = "${var.env}_${var.workspace}"
  username                = "${var.root_username}"
  password                = "${random_id.root_password.b64}"
  db_subnet_group_name    = "${aws_db_subnet_group.this.name}"
  parameter_group_name    = "${var.param_group_name}" …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services terraform

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

在postgreSQL中将NULL值插入TIMESTAMP字段

我在 MySQL 中有以下数据库模式,我已使用 ORM 将其复制到 PostgreSQL 中:

CREATE TABLE IF NOT EXISTS users (
id               BIGINT NOT NULL DEFAULT NEXTVAL ('users_seq'),
email            VARCHAR(255) NOT NULL DEFAULT '',
password         VARCHAR(255) NOT NULL DEFAULT '',
last_name        VARCHAR(255) NOT NULL DEFAULT '',
first_name       VARCHAR(255) NOT NULL DEFAULT '',
created          TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated          TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
Run Code Online (Sandbox Code Playgroud)

);

我正在尝试执行以下查询

INSERT INTO users (id, email, password,first_name, last_name, created, updated)
VALUES (1, 'user@gmail.com', 'pass', 'user', 'user', NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

这会导致错误 …

mysql postgresql

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