Go:内联字符串拼接

huangapple go评论75阅读模式
英文:

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()))

huangapple
  • 本文由 发表于 2015年10月23日 05:49:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/33291374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定