哔哩哔哩-SDK-Go 使用教程
1. 项目的目录结构及介绍
bilibili-sdk-go/
├── api/
│ ├── api.go
│ └── ...
├── client/
│ ├── bili_client.go
│ ├── comm_client.go
│ └── ...
├── config/
│ ├── config.go
│ └── ...
├── internal/
│ ├── util/
│ │ └── ...
│ └── ...
├── proto/
│ ├── dm/
│ │ └── ...
│ └── ...
├── .gitignore
├── API.md
├── LICENSE
├── README.md
├── go.mod
├── go.sum
├── types.go
├── util.go
└── util_test.go
目录结构介绍
api/
: 包含与哔哩哔哩API交互的接口实现。client/
: 包含客户端相关的代码,如bili_client.go
和comm_client.go
。config/
: 包含配置文件相关的代码,如config.go
。internal/
: 包含内部工具和辅助函数。proto/
: 包含协议相关的代码,如弹幕相关的代码。.gitignore
: Git忽略文件。API.md
: API文档。LICENSE
: 项目许可证。README.md
: 项目介绍和使用说明。go.mod
和go.sum
: Go模块文件。types.go
: 数据类型定义。util.go
和util_test.go
: 工具函数和测试。
2. 项目的启动文件介绍
项目的启动文件通常是main.go
,但在本项目中,由于是一个SDK,没有传统的main.go
文件。相反,主要的入口点是client/bili_client.go
,它包含了初始化和配置客户端的逻辑。
client/bili_client.go
package client
import (
"bilibili-sdk-go/config"
"bilibili-sdk-go/api"
)
type BiliClient struct {
Config *config.Config
Api *api.Api
}
func NewBiliClient(cfg *config.Config) *BiliClient {
return &BiliClient{
Config: cfg,
Api: api.NewApi(cfg),
}
}
启动流程
- 加载配置文件。
- 初始化
BiliClient
。 - 调用API接口进行交互。
3. 项目的配置文件介绍
项目的配置文件通常位于config/config.go
中,它定义了项目的配置结构和加载配置的方法。
config/config.go
package config
import (
"os"
"encoding/json"
)
type Config struct {
ApiKey string `json:"api_key"`
ApiSecret string `json:"api_secret"`
}
func LoadConfig(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var cfg Config
decoder := json.NewDecoder(file)
err = decoder.Decode(&cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
配置文件示例
{
"api_key": "your_api_key",
"api_secret": "your_api_secret"
}
配置文件加载
cfg, err := config.LoadConfig("config.json")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
client := client.NewBiliClient(cfg)
通过以上步骤,您可以成功加载配置文件并初始化客户端,进而使用哔哩哔哩SDK进行API调用。