Implementation of os.args

142 views
Skip to first unread message

Hamish MacDonald

unread,
Aug 15, 2021, 12:30:08 PM8/15/21
to golang-nuts

Hi all

I am trying to understand the implementation of os.Args().

The os package says this is implemented in the runtime package

file runtime.go contains:

//go:linkname os_runtime_args os.runtime_args
func os_runtime_args() []string { return append([]string{}, argslice...) }

My question is:

Where in the implementation is argslice populated?

Thanks for any insight someone can provide.

Cheers

Hamish

Ian Lance Taylor

unread,
Aug 15, 2021, 12:45:28 PM8/15/21
to Hamish MacDonald, golang-nuts
On Sun, Aug 15, 2021 at 9:30 AM Hamish MacDonald
<hamish.m...@servian.com> wrote:
>
> I am trying to understand the implementation of os.Args().
>
> The os package says this is implemented in the runtime package
>
> file runtime.go contains:
> var argslice []string
>
> //go:linkname os_runtime_args os.runtime_args
> func os_runtime_args() []string { return append([]string{}, argslice...) }
>
> My question is:
>
> Where in the implementation is argslice populated?

If you grep for argslice in the runtime package sources you will see
that it is set by the goargs function based on argc and argv. argc
and argv are set by the function args, which is called by the startup
code which is written in assembler. For amd64/Linux you can see the
call to args from runtime·rt0_go in asm_amd64.s. The argc and argv
values are provided by the operating system when the program starts.

Ian
Reply all
Reply to author
Forward
0 new messages