死磕row.Cells undefined (type *xlsx.Row has no field or method

当开始写这篇文章时,终于松一口气了。

目录

问题描述与排查过程

解决方案


问题描述与排查过程

接手了以前同事开发的项目,编译项目报错如下:

死磕row.Cells undefined (type *xlsx.Row has no field or method

提示xlFile.Sheet[readInfo.SheetName].Rows undefined (type *xlsx.Sheet has no field or method Rows)

row.Cells undefined (type *xlsx.Row has no field or method Cells)

rows[0].Cells undefined (type *xlsx.Row has no field or method Cells)等等

用到了"github.com/tealeg/xlsx"这个库,代码中情形如下:

死磕row.Cells undefined (type *xlsx.Row has no field or method

提示Unresolved reference 'Rows' ,Unresolved reference 'Cells' 。

然后排查,层层点击,从xlFile.Sheet中的Sheet点进去看到它的类型:

死磕row.Cells undefined (type *xlsx.Row has no field or method

再点到Sheet结构体中,看了下,确实没有Rows这个属性(此时所在文件路径GoWork\src\github.com\tealeg\xlsx\sheet.go),然后去看github.com/tealeg/xlsx库下面找到这个Sheet.go,master分支的如下:

死磕row.Cells undefined (type *xlsx.Row has no field or method

确实没有Rows这个属性,那么可以由此推断,所用的库没有导正确。那么再看看v2分支的:

死磕row.Cells undefined (type *xlsx.Row has no field or method

这不OK了,这不就是报红地方所缺少的。。。

解决方案

猜想手动拉新分支的代码放到$GOPATH下再编译应该就OK了。试试看:

死磕row.Cells undefined (type *xlsx.Row has no field or method

进入项目目录下build,编译成功了,二进制文件出来了!!!

 

总结:

由此可见,报红不能引入一些属性之类的报错,如果点进去是源码文件中缺少的,那就可以从第三方库本身找突破口从而解决!