有没有什么合理的方法可以判断在windows平台上使用go语言能否成功删除文件?我尝试使用各种标志调用 os.OpenFile 包括使用 os.O_TRUNC 所有调用都成功但删除将在文件使用时失败。我还尝试使用特定于 Windows 的锁定机制(即 kern.NewProc("LockFileEx") 等),这些似乎成功获得排他锁,但 os.Remove(解锁后)将失败。我意识到如果在测试和删除调用之间,如果另一个进程打开文件删除仍然会失败,但这里不是这种情况,我的测试我知道文件正在使用但我找不到测试来告诉我那个(除了实际调用 os.Remove)
我正在尝试更新 Windows 服务器上的主机文件,并尝试使用 powershell 中的 heredoc 来完成。
我不明白为什么我的结果在每个主机条目中的每个字符之间都有额外的空格。
我正在从 Linux 移植一些脚本。
PS C:\Users\Administrator> cat C:\Users\Administrator\AppData\Local\Temp\etchosts.ps1
@"
127.0.0.1 src.example.com
127.0.0.2 builds.example.com
127.0.0.3 ti.example.com
127.0.0.4 jira.example.com
"@ >>C:\Windows\System32\drivers\etc\hosts
PS C:\Users\Administrator> powershell C:\Users\Administrator\AppData\Local\Temp\etchosts.ps1
PS C:\Users\Administrator> cat C:\Windows\System32\drivers\etc\hosts
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address …Run Code Online (Sandbox Code Playgroud)