如何在 Go 中确定可执行文件状态
给定一个 os.FileInfo 实例,您可能需要检查文件是否在 Go 中可执行去。这需要破译 os.FileInfo.Mode() 中的权限位。
测试用例:
#!/usr/bin/env bash mkdir -p test/foo/bar touch test/foo/bar/{baz.txt,quux.sh} chmod +x test/foo/bar/quux.sh
<code class="go">import ( "os" "path/filepath" "fmt" )</code>
解决方案:
文件的可执行性由存储在 os.FileMode.Perm() 中的 Unix 权限位决定。这些位形成 9 位位掩码(八进制 0777)。
Unix 权限位含义:
rwxrwxrwx
对于每个用户类:
- 所有者:位掩码 0100
- 组:位掩码 0010
- 其他:位掩码 0001
检查可执行性的函数:
-
可由所有者执行:
<code class="go">func IsExecOwner(mode os.FileMode) bool { return mode&0100 != 0 }</code>
-
由组执行:
<code class="go">func IsExecGroup(mode os.FileMode) bool { return mode&0010 != 0 }</code>
-
可由其他人执行:
<code class="go">func IsExecOther(mode os.FileMode) bool { return mode&0001 != 0 }</code>
-
可由任何人执行:
<code class="go">func IsExecAny(mode os.FileMode) bool { return mode&0111 != 0 }</code>
-
所有人可执行:
<code class="go">func IsExecAll(mode os.FileMode) bool { return mode&0111 == 0111 }</code>
<code class="go">func main() { filepath.Walk("test", func(path string, info os.FileInfo, err error) error { if err != nil || info.IsDir() { return err } fmt.Printf("%v %v", path, IsExecAny(info.Mode().Perm())) } }</code>
预期输出:
test/foo/bar/baz.txt false test/foo/bar/quux.txt true
以上是如何判断Go文件是否可执行?的详细内容。更多信息请关注PHP中文网其他相关文章!

Go的"strings"包提供了丰富的功能,使字符串操作高效且简单。1)使用strings.Contains()检查子串。2)strings.Split()可用于解析数据,但需谨慎使用以避免性能问题。3)strings.Join()适用于格式化字符串,但对小数据集,循环使用 =更有效。4)对于大字符串,使用strings.Builder构建字符串更高效。

Go语言使用"strings"包进行字符串操作。1)拼接字符串使用strings.Join函数。2)查找子串使用strings.Contains函数。3)替换字符串使用strings.Replace函数,这些函数高效且易用,适用于各种字符串处理任务。

资助bytespackageingoisesential foreffited byteSemanipulation,uperingFunctionsLikeContains,index,andReplaceForsearchingangingAndModifyingBinaryData.itenHancesperformanceNandCoderAceAnibility,MakeitiTavitalToolToolToolToolToolToolToolToolToolForhandLingBinaryData,networkProtocols,networkProtocoLss,networkProtocols,andetFilei

Go语言使用"encoding/binary"包进行二进制编码与解码。1)该包提供binary.Write和binary.Read函数,用于数据的写入和读取。2)需要注意选择正确的字节序(如BigEndian或LittleEndian)。3)数据对齐和错误处理也是关键,确保数据的正确性和性能。

1)usebybytes.joinforconcatenatinges,2)bytes.bufferforincrementalWriter,3)bytes.indexorbytes.indexorbytes.indexbyteforsearching bytes.bytes.readereforrednerncretinging.isnchunk.ss.ind.inc.softes.4)

theencoding/binarypackageingoiseforporptimizingBinaryBinaryOperationsDuetoitssupportforendiannessessandefficityDatahandling.toenhancePerformance:1)usebinary.nativeendiandiandiandiandiandiandiandian nessideendian toavoid avoidByteByteswapping.2)

Go的bytes包主要用于高效处理字节切片。1)使用bytes.Buffer可以高效进行字符串拼接,避免不必要的内存分配。2)bytes.Equal函数用于快速比较字节切片。3)bytes.Index、bytes.Split和bytes.ReplaceAll函数可用于搜索和操作字节切片,但需注意性能问题。

字节包提供了多种功能来高效处理字节切片。1)使用bytes.Contains检查字节序列。2)用bytes.Split分割字节切片。3)通过bytes.Replace替换字节序列。4)用bytes.Join连接多个字节切片。5)利用bytes.Buffer构建数据。6)结合bytes.Map进行错误处理和数据验证。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

记事本++7.3.1
好用且免费的代码编辑器

WebStorm Mac版
好用的JavaScript开发工具