Setting process name (as seen by `ps`) in Go

前端 未结 3 1878
名媛妹妹
名媛妹妹 2020-12-16 22:28

The following (rightfully) doesn\'t work:

package main

import (
        \"os\"
        \"time\"
)

func main() {
        os.Args[0] = \"custom name\"
               


        
3条回答
  •  清酒与你
    2020-12-16 22:42

    I don't think that "process title" is a well defined term. Anyway, what has Ruby to do with Go? The documentation for os.Args doesn't mention any "process title", nor it says any magic will happen on assigning to a slice item. The later is actually a general property of Go. There's no magic getters/setters for struct fields, variables of array/slice items, so a simple assignment simply assigns and does nothing more and cannot do anything more.

    In short, the lack of magic is the expected, correct behavior.

    For fiddling with process properties other than the portably accessible ones via the 'os' package, one has to use package 'syscall' in a platform specific way. But then the build constraints (discussed here) can help to correctly handle stuff across platforms.

提交回复
热议问题