// GetParentDirectory 获取上级目录
func GetParentDirectory(dirctory string) string {
if len(dirctory) == 0 {
dir, dirErr := exec.LookPath(os.Args[0])
if dirErr != nil {
log.WithFields(log.Fields{"error": dirErr}).Warn("当前目录获取-GetParentDirectory")
return ""
}
dirctory = strings.Replace(dir, "\\", "/", -1)
}
return common.Substr(dirctory, 0, strings.LastIndex(dirctory, "/"))
}
Go重新PHP的substr方法:
// Substr substr()
func Substr(str string, start uint, length int) string {
if start < 0 || length < -1 {
return str
}
switch {
case length == -1:
return str[start:]
case length == 0:
return ""
}
end := int(start) + length
if end > len(str) {
end = len(str)
}
return str[start:end]
}