Golang函數無法正確讀取檔案名
所以,我有一個包含多個 .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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

在BeegoORM框架下,如何指定模型關聯的數據庫?許多Beego項目需要同時操作多個數據庫。當使用Beego...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

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