英文:
Go: Inline string concatenation
问题
我需要使用os.Open
来打开一个文件。我有路径./XML/
和文件名foo.xml
,每次都会变化。也就是说,我有一个从目录中读取的xml文件数组,我想逐个打开它们(或者使用线程,无所谓)。
我需要简单地将./XML/
添加到SOMETHING.xml
。在Java中,这很简单:
String a = "whatever", b = "whatever";
doSomething(a + b);
在Go语言中如何实现这个功能?谷歌没有给我找到答案。
file, err := os.Open(????????????)
英文:
I need to call os.Open
to open a file. I have the path ./XML/
and the filename foo.xml
, which changes each time. That is I have an array of xml files read from a dir which I am trying to open one at a time (or threaded, doesn't matter).
I need to simple add ./XML/
to SOMETHING.xml
. In Java this is trivial,
String a = "whatever", b = "whatever";
doSomething(a + b);
How is this done in Go? Google has failed me.
file, err := os.Open(????????????)
答案1
得分: 9
最便携的路径拼接方法是使用filepath.Join
:
import "path/filepath"
file, err := os.Open(filepath.Join("XML", fileinfo.Name()))
英文:
The most portable way to do path concatenation is by using filepath.Join
:
import "path/filepath"
file, err := os.Open(filepath.Join("XML", fileinfo.Name()))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论