自動創建目錄
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,5 +1,5 @@
 | 
			
		||||
# Go
 | 
			
		||||
data/
 | 
			
		||||
data
 | 
			
		||||
 | 
			
		||||
# ---> Python
 | 
			
		||||
# Byte-compiled / optimized / DLL files
 | 
			
		||||
 
 | 
			
		||||
@@ -3,12 +3,22 @@ package configs
 | 
			
		||||
import (
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	_ "github.com/mattn/go-sqlite3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 使用SQLite3初始化數據庫
 | 
			
		||||
func init() {
 | 
			
		||||
 | 
			
		||||
	// 設置日誌顯示文件名和行號
 | 
			
		||||
	log.SetFlags(log.Lshortfile | log.LstdFlags)
 | 
			
		||||
 | 
			
		||||
	// 原生golang 創建 data 目錄不存在則創建
 | 
			
		||||
	if _, err := os.Stat("data"); os.IsNotExist(err) {
 | 
			
		||||
		os.Mkdir("data", os.ModePerm)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 初始化數據庫
 | 
			
		||||
	db, err := sql.Open("sqlite3", "data/sqlite3.db")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user