如何使用golang更改符号链接的时间戳?

os.Chtimes always to follow symlinks and change the real files timestamp. Is there a method to change the symlinks timestamp in?

Just like touch -h does.

Not sure it's possible, at least from the syscall package.

Looking at the source-code for say syscall.Chtimes:

func Chtimes(name string, atime time.Time, mtime time.Time) error {
    var utimes [2]syscall.Timespec
    utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
    utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
    if e := syscall.UtimesNano(fixLongPath(name), utimes[0:]); e != nil {
        return &PathError{"chtimes", name, e}
    }
    return nil
}

duplicating this code - and removing the fixLongPath call which I assumed followed the symlinks - still affects the target file, not the source symlink.

Even trying this operation on a symlink which points to a non-existent file, returns a runtime error no such file or directory.

A CGO pkg - could, but that seems overkill.

You can check if a symlink exists and if so, remove it and create another one.

if _, err := os.Lstat(symlinkPath); err == nil {
  os.Remove(symlinkPath)
}

err := os.Symlink(filePath, symlinkPath)
if err != nil {
  fmt.Println(err)
}