-1

为清楚起见,大部分代码都被删除以更多地关注问题。我有一个使用以下代码go-micro调用的服务器文件:main.go

package main

import (
    "fmt"
    "strings"

    "github.com/micro/go-micro"
    "github.com/micro/go-micro/server"

    proto "mypkg/proto"
)

const serviceName = "SRV"

func main() {
    service := micro.NewService(
        micro.Name(strings.ToLower(serviceName)),
        micro.Server(
            server.NewServer(
                server.Name(strings.ToLower(serviceName))
            ),
        ),
    )

    service.Init()

    if err := proto.RegisterSRVServiceHandler(service.Server(), new(SRVService)); err != nil {
        panic(err)
    }

    if err := service.Run(); err != nil {
        panic(err)
    }
}

SRVService在另一个名为srv_service.go(具有相同包名)的文件中,我不知道如何导入它:

package main

import (
    "context"
    proto "mypkg/proto"
)

type SRVService struct{}

func (g *SRVService) AddUser(ctx context.Context, req *proto.AddUserRequest, rsp *proto.AddUserResponse) error {
    rsp.UserId = "12312331231"
    return nil
}

我应该如何SRVService访问main.go

4

1 回答 1

0

If they're in the same folder, they're part of the same package (their package declaration must reflect that). If they are part of the same package, you don't need to import anything. You can refer to all identifiers (being exported or not) from the package.

The main package is special though. If your main package consists of multiple files, you have to list all when running or building the app, e.g.:

go run srv_service.go main.go
于 2019-07-29T12:23:16.800 回答