dsc56927 2016-07-14 14:47
浏览 27
已采纳

如果您知道Go中的群组名称,请获取提示

I created a group on a Linux machine. Now I am trying to call os.Chown to change a file's ownership to that new group.

os.Chown requires me to know the uid and the gid:

func Chown(name string, uid, gid int) error

How can I get the gid for my group? I tried using user.Lookup("groupname"), but I got "unknown user groupname"

I can call os.Getgroups, but this only returns me an array of group IDs - it doesn't tell me anything about the mapping between a group name and the group id.

I am guessing there is a Unix utility I can shell out to (parse the result of calling id) but I'd rather not do that if I can help it.

  • 写回答

1条回答 默认 最新

  • dt102282 2016-07-14 14:57
    关注

    Update: As of August 16, 2016, Go 1.7 is released with support for LookupGroup. Upgrading to Go 1.7 is recommended if you want to access information about POSIX groups.

    There is a LookupGroup function in the Go 1.7 betas, but it was added only this February and isn't in any released version of Go, so you're kind of out of luck.

    As far as I can see your options are:

    1. Upgrade to a beta.
    2. Wait for the 1.7 release, scheduled for August.
    3. Write your own cgo function that calls getgrnam for the information you need (tricky)
    4. Write your own code that parses /etc/group (relatively easy... as long as you can guarantee that the info you need actually comes from that file and not LDAP or something).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)