所以,我有一個包含多個 .csv 檔案的儲存庫,它們包含資料庫的表格架構。我編寫了一段 Golang 程式碼,它從儲存庫中取得文件名稱列表,然後打開這些文件,讀取內容並建立 MySQL CREATE 查詢。
我面臨的問題是,對於某些 .csv 文件,Golang 程式碼最終會錯誤地讀取標題,這會導致後期出現問題。例如,有一些名為 config_hr.csv、config_oe.csv、contribution_analysis.csv 的檔案被讀取為 onfig_hr.csv、onfig_oe.csv、ontribution_analysi.csv。如果我將名稱大寫,這個問題似乎可以解決,但是在我們專案的後期階段還會出現許多其他問題。
這是某種程式設計問題嗎?我已經檢查了 Windows、Mac 和 Linux 上的程式碼,Golang 版本是最新的 v1.21,任何幫助或見解將不勝感激!
讀取 CSV 檔案名稱的 Golang 程式碼片段
entries, err := FileEntry.Readdir(0) if err != nil { log.Fatal(err) } // Now, open all the files one by one, and extract the content of the files. // Then modify the resultant string to be of MySQL compatibility. for _, e := range entries { // Mimicking the SQL Query of Table Creation query. Query_String := ("CREATE TABLE IF NOT EXISTS " + strings.ToLower(strings.Trim(strings.Replace(e.Name(), " ", "_", -1), ".csv")) + " (\n") fmt.Println("Opening -- " + file_folder + "/" + e.Name()) file, err := os.Open(file_folder + "/" + e.Name()) if err != nil { log.Fatal(err) } defer file.Close() // Reading the CSV file from path. reader := csv.NewReader(file) records, err := reader.ReadAll() if err != nil { log.Fatal(err) }
正確答案
將 string.Trim
函數替換為下列函數。
// getFileNameWithoutExtension takes a file path as input and returns // the file name without its extension. It utilizes the filepath package // to extract the base name and then removes the extension. func getFileNameWithoutExtension(filePath string) string { // Get the base name of the file path (including extension) baseName := filepath.Base(filePath) // Calculate the length to remove the extension from the base name // and obtain the file name without extension fileNameWithoutExtension := baseName[:len(baseName)-len(filepath.Ext(baseName))] // Return the file name without extension return fileNameWithoutExtension }
範例程式碼:
Query_String := ("CREATE TABLE IF NOT EXISTS " + strings.ToLower(getFileNameWithoutExtension(strings.Replace(e.Name(), " ", "_", -1))) + " (\n")
以上是Golang函數無法正確讀取檔案名的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

golang ...

Go語言中如何對比並處理三個結構體在Go語言編程中,有時需要對比兩個結構體的差異,並將這些差異應用到第�...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中