dongsu1539
2018-10-03 07:10
浏览 449
已采纳

来自不同文件的golang接口使用方法

I've method which I want to provide some interface to make it more easier to test

This is the function

File A

func readFile(s source) ([]byte, error) {
        p := fs.GetPath()
        file, err := ioutil.ReadFile(p + "/" + s.path + "/" + "rts.yaml")
        if err != nil {
            return yamlFile, fmt.Errorf("erro reading file : %s", err.Error())
        }
        return file, err
    }

Now I add for it struct

type source struct{
    path string
}

And the interface that the readFile is implementing

type fileReader interface {
    readFile(path string) ([]byte, error)
}

And now I need to call this function from another file but Im getting error while doing this

File B

type source struct {
    path string
}


a := source{}

yamlFile, err := readFile(a)

what am I missing here ?

图片转代码服务由CSDN问答提供 功能建议

我有方法,我想提供一些界面以使其更易于使用 测试

这是函数

文件A

   func readFile(s源)([] byte,错误){
p:= fs.GetPath()
文件,err:= ioutil.ReadFile(p +“ /” + s.path +“ /” +“ rts  .yaml“)
如果错误!= nil {
返回yamlFile,fmt.Errorf(”错误读取文件:%s“,err.Error())
} 
返回文件,错误
} 
    
 
 

现在我为它添加struct

  type source struct {
 path string 
} 
   
 
 

以及 readFile 的接口实现

 类型 fileReader接口{
 readFile(路径字符串)([]字节,错误)
} 
   
 
 

现在我需要从另一个文件中调用此函数,但是Im 这样做时出错

文件B

 类型源结构 {
路径字符串
} 
 
 
a:=源{} 
 
yamlFile,错误:= readFile(a)
   
 
 

什么是 我在这里想念吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douhunbei0166 2018-10-03 10:17
    已采纳

    Import the package containing the source struct in File A and then use that struct to initialize the variable after that pass the variable to the readFile function.

    File B

    import A
    a := A.Source{}
    

    Because source struct in File A is different from source struct in File B. And source struct of File A is implementing the interface that's why you need to import the source struct and then pass it into the function.

    One this should be noticed that to make any struct or function exportable you should start the struct or fucntion name with upper case letter.

    File A

    // make struct exportable
    type Source struct{
        path string
    }
    

    implemented the interface which is different from

    File B

    type source struct{
        path string
    }
    

    which does not implemented the interface.

    Edited

    File A

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "os"
    )
    
    type Source struct {
        Path string
    }
    
    type fileReader interface {
        readOneFile() ([]byte, error)
    }
    
    func(s Source) readOneFile() ([]byte, error) {
        cwd, err := os.Getwd()
        file, err := ioutil.ReadFile(fmt.Sprintf("%s/file.txt", cwd))
        if err != nil {
            return nil, fmt.Errorf("erro reading file : %s", err.Error())
        }
        return file, err
    }
    

    File B

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        s := Source{}
        data, err := s.readOneFile()
        if err != nil {
            fmt.Errorf("Error in reading the file")
        }
        fmt.Println(string(data))
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题