go-exiftool 项目教程
1. 项目的目录结构及介绍
go-exiftool/
├── README.md
├── go.mod
├── go.sum
├── exiftool/
│ ├── exiftool.go
│ ├── exiftool_test.go
│ └── options.go
├── testdata/
│ └── 20190404_131804.jpg
└── examples/
└── example.go
- README.md: 项目介绍和使用说明。
- go.mod 和 go.sum: Go 模块文件,用于管理依赖。
- exiftool/: 核心代码目录,包含 ExifTool 的封装逻辑。
- exiftool.go: 主要功能实现。
- exiftool_test.go: 测试文件。
- options.go: 配置选项。
- testdata/: 测试数据目录,包含示例图片。
- examples/: 示例代码目录,展示如何使用该库。
2. 项目的启动文件介绍
项目的启动文件位于 exiftool/exiftool.go
,主要功能包括:
- NewExiftool(): 初始化 ExifTool 实例。
- Close(): 关闭 ExifTool 实例。
- ExtractMetadata(): 提取文件的元数据。
- WriteMetadata(): 写入文件的元数据。
示例代码:
package main
import (
"fmt"
"github.com/barasher/go-exiftool"
)
func main() {
et, err := exiftool.NewExiftool()
if err != nil {
fmt.Printf("Error when initializing: %v\n", err)
return
}
defer et.Close()
fileInfos := et.ExtractMetadata("testdata/20190404_131804.jpg")
for _, fileInfo := range fileInfos {
if fileInfo.Err != nil {
fmt.Printf("Error concerning %v: %v\n", fileInfo.File, fileInfo.Err)
continue
}
for k, v := range fileInfo.Fields {
fmt.Printf("[%v] %v\n", k, v)
}
}
}
3. 项目的配置文件介绍
项目没有显式的配置文件,但可以通过 SetExiftoolBinaryPath
函数指定 ExifTool 可执行文件的路径。
示例代码:
package main
import (
"fmt"
"github.com/barasher/go-exiftool"
)
func main() {
opts := []exiftool.Option{
exiftool.SetExiftoolBinaryPath("/path/to/exiftool"),
}
et, err := exiftool.NewExiftool(opts...)
if err != nil {
fmt.Printf("Error when initializing: %v\n", err)
return
}
defer et.Close()
// 其他代码...
}
通过这种方式,可以灵活地配置 ExifTool 的路径,以适应不同的环境。