小编des*_*Joe的帖子

如何在Go中使用反射设置作为指向字符串的指针的结构成员

我正在尝试使用反射来设置指针。elasticbeanstalk.CreateEnvironmentInput有一个SolutionStackName类型为 的字段*string。当我尝试设置任何值时出现以下错误:

panic: reflect: call of reflect.Value.SetPointer on ptr Value
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

    ...
newEnvCnf := new(elasticbeanstalk.CreateEnvironmentInput)
checkConfig2(newEnvCnf, "SolutionStackName", "teststring")
    ...
func checkConfig2(cnf interface{}, key string, value string) bool {
    log.Infof("key %v, value %s", key, value)

    v := reflect.ValueOf(cnf).Elem()
    fld := v.FieldByName(key)

    if fld.IsValid() {
        if fld.IsNil() && fld.CanSet() {
            fld.SetPointer(unsafe.Pointer(aws.String(value)))
//aws.String returns a pointer

...
Run Code Online (Sandbox Code Playgroud)

这是日志输出

time="2016-02-20T23:54:52-08:00" level=info msg="key [SolutionStackName], value teststring" 
    panic: reflect: call of reflect.Value.SetPointer on ptr Value [recovered]
        panic: reflect: call of …
Run Code Online (Sandbox Code Playgroud)

string reflection struct pointers go

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

无法从 Spring Boot jar 读取文本文件

我正在尝试读取 Spring Boot 控制台应用程序的资源文件夹中的文件,但出现文件未找到异常。

这是我的 pom

<resource>
    <directory>src/main/resources</directory>
    <includes>
      <include>**/*.*</include>
    </includes>
  </resource>
Run Code Online (Sandbox Code Playgroud)

这是例外:

java.io.FileNotFoundException: class path resource [9.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/abc/Documents/workspace-sts-3.8.4.RELEASE/xyz/target/xyz-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/9.txt
Run Code Online (Sandbox Code Playgroud)

我打开了 xyz-0.0.1-SNAPSHOT.jar 文件,9.txt 位于 BOOT-INF/classes 文件夹中。

谢谢,-dj

spring-boot

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

为什么Go认为我没有使用声明变量?

我是Go的新手,想知道为什么Go认为我没有使用vpcGrp?你会怎么编码?

这是我的代码:

var vpcGrp *ec2.SecurityGroup
grpExists := false
resp, err := svc.DescribeSecurityGroups(params)
if err != nil {
    log.Error(err)
} else {
    for _, v := range resp.SecurityGroups {
        if *v.GroupName == grpName {
            log.Debug("VPC Security Group Found [", grpName, "]")
            vpcGrp = v
            grpExists = true
        }
    }
}

if !grpExists {
    vpcGrp, err := createDbSecurityGrp(grpName, vpcId, region)
    if err != nil {
        log.Error(err)
        return nil, err
    }
}

return vpcGrp, nil
Run Code Online (Sandbox Code Playgroud)

go

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

如何将Map传递给Go中的回调函数

我正在尝试编写一个Go程序来遍历目录并找到某个文件并将该信息存储在Map中.这是我到目前为止所拥有的.我不知道如何将Map传递给访问函数,因为它是一个回调函数.

..
type MyFile struct {
    Name       string
    FilePath    string
    FileMD5Hash [16]byte
}

func visit(path string, f os.FileInfo, err error) error {

    fileName := f.Name()
    if !f.IsDir() && strings.Contains(strings.ToLower(fileName), "myfile") {
        df := parseFile(path)
        fmt.Printf("Visited: %s [%x], %s, %s\n", df.FilePath)
    }

    return nil
}

func parseFile(path string)...

func check(e error) ...

func WalkDir(path string) {
    err := filepath.Walk(path, visit)
    fmt.Printf("filepath.Walk() returned %v\n", err)
}
Run Code Online (Sandbox Code Playgroud)

go

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

postgresql 中的条件插入语句

我正在尝试运行条件插入语句,但遇到问题。声明如下:

insert into category_content (category_id, content_id, content_type_id, priority) (select 29, id, 1, 1 from article where blog_id = 80) 
where not exists(
select * from category_content where category_id = 29 and firstname in (select id from article where blog_id = 80)
);
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

insert into category_content (category_id, content_id, content_type_id, priority) (select 29, id, 1, 1 from article where blog_id = 80) 
where not exists(
select * from category_content where category_id = 29 and firstname in (select id from article where …
Run Code Online (Sandbox Code Playgroud)

sql postgresql

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

标签 统计

go ×3

pointers ×1

postgresql ×1

reflection ×1

spring-boot ×1

sql ×1

string ×1

struct ×1