duanjizhan9353 2016-09-08 18:28
浏览 108

如何在Golang中为结构创建对象

File1.go

Package abc

type ECA struct {
    *CA
    obcKey          []byte
    obcPriv, obcPub []byte
    gRPCServer      *grpc.Server
}


type ECAP struct {
    eca *ECA
}


func (ecap *ECAP) ReadCACertificate(ctx context.Context, in *pb.Empty) (*pb.Cert, error) {
    Trace.Println("gRPC ECAP:ReadCACertificate")

    return &pb.Cert{Cert: ecap.eca.raw}, nil
}

File2.go

package main

import "abc"

var ecap abc.ECAP //creating instance

func main() {
    err = ecap.ReadCACertificate(floo,floo)
}

I am a newbie. I want to create instance of ECAP struct and call ReadCACertificate method. Right now i am creating like this "var ecap abc.ECAP //creating instance" which is giving "nil" and nil pointer error.

Can anyone help how to call the ReadCACertificate method in efficient way.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongran1779 2016-09-08 18:44
    关注

    You need to import the second file from your $GOPATH. And your function return 2 values, not just an error.

    package main
    
    
    import (
         "$RELATIVE_PATH_FROM_GOPATH/abc"
    )
    
    
    var ecap := ECAP{new(ECA)} //initialize your struct, since it has pointers
    
    func main() {
        cert, err = ecap.ReadCACertificate(floo,floo)
    }
    

    File 1

    type ECAP struct {
        Eca *ECA //uppercase make field be exported
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器