所以,我有一个包含多个 .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中文网其他相关文章!

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang在并发性上优于C ,而C 在原始速度上优于Golang。1)Golang通过goroutine和channel实现高效并发,适合处理大量并发任务。2)C 通过编译器优化和标准库,提供接近硬件的高性能,适合需要极致优化的应用。

选择Golang的原因包括:1)高并发性能,2)静态类型系统,3)垃圾回收机制,4)丰富的标准库和生态系统,这些特性使其成为开发高效、可靠软件的理想选择。

Golang适合快速开发和并发场景,C 适用于需要极致性能和低级控制的场景。1)Golang通过垃圾回收和并发机制提升性能,适合高并发Web服务开发。2)C 通过手动内存管理和编译器优化达到极致性能,适用于嵌入式系统开发。

Golang在编译时间和并发处理上表现更好,而C 在运行速度和内存管理上更具优势。1.Golang编译速度快,适合快速开发。2.C 运行速度快,适合性能关键应用。3.Golang并发处理简单高效,适用于并发编程。4.C 手动内存管理提供更高性能,但增加开发复杂度。

Golang在Web服务和系统编程中的应用主要体现在其简洁、高效和并发性上。1)在Web服务中,Golang通过强大的HTTP库和并发处理能力,支持创建高性能的Web应用和API。2)在系统编程中,Golang利用接近硬件的特性和对C语言的兼容性,适用于操作系统开发和嵌入式系统。

Golang和C 在性能对比中各有优劣:1.Golang适合高并发和快速开发,但垃圾回收可能影响性能;2.C 提供更高性能和硬件控制,但开发复杂度高。选择时需综合考虑项目需求和团队技能。

Golang适合高性能和并发编程场景,Python适合快速开发和数据处理。 1.Golang强调简洁和高效,适用于后端服务和微服务。 2.Python以简洁语法和丰富库着称,适用于数据科学和机器学习。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版