修改了一下。
package main
import (
"time"
"fmt"
"strings"
)
const (
DefaultTimeFormat = "2006-01-02 15:04:05.999999999"
)
func main() {
timeStr := time.Now().Format(DefaultTimeFormat)
timeStr = strings.Replace(timeStr, ".", ",", 1)
fmt.Printf("TimeNow: %s\n", timeStr)
timeStr = "2013-11-26 19:08:17,583492285"
fmt.Println("TimeTransferBefore:", timeStr)
timeStr = strings.Replace(timeStr, ",", ".", 1)
t, err := time.Parse(DefaultTimeFormat, timeStr)
if err != nil {
fmt.Println(err)
return
}
timeStr = t.Format(DefaultTimeFormat)
timeStr = strings.Replace(timeStr, ".", ",", 1)
fmt.Println("TimeTransferAfter:", timeStr)
}
试试这个行不行
import "time"
import "fmt"
import "strings"
const DefaultTimeFormat1 = "2006-01-02T15:04:05,999999999"
const DefaultTimeFormat2 = "2006-01-02T15:04:05.999999999"
func main() {
fmt.Printf("Time: %s\n, time.Now().Format(DefaultTimeFormat1))
// Transfer 2013-11-26 19:08:17,583492285 to time
timeStr = "2013-11-26 19:08:17,583492285"
timeStr = strings.Replace(timeStr, ",", ".", 1)
t, err := time.Prase(timeStr, DefaultTimeFormat2)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("TimeTransfer: %s\n, t.Format(DefaultTimeFormat1))
}
或者使用 fmt.Errorf(format string, a ...interface{})
import (
"fmt"
)
...
var err = fmt.Errorf("invalid code: %d", val)
...