I know that os.Args[0] is the path to my program, but can I do os.Args[0] to be my working directory in Goland runner?
No, you cannot make os.Args[0]
to be the working directory, since as you correctly identified, it's used to locate your program on disk.
There are two changes you can make:
- change your code to stop relying on
os.Args[0]
and start using os.Getwd()
to get the working directory. Then you can edit your Run Configuration
via Run | Edit Configurations... | <name of the run configuration> | Working Directory
parameter.
- if you really want you binary to be in a certain directory, then you can go to
Run | Edit Configurations... | <name of the run configuration> | Output directory
to have the IDE run the compiler and place the executable wherever you need to.
If you need to make these changes for all future Run Configurations, then edit Run | Edit Configurations... | Templates | Go Build
, or Go Test
and change either the Working Directory
or the Output Directory
fields. Existing Run Configurations
will not be changed.