Re: [go-nuts] Current working directory + Mac OS (pathtome)

1,601 views
Skip to first unread message
Message has been deleted

roger peppe

unread,
Jan 8, 2011, 2:09:29 PM1/8/11
to Michael Thomas Lee, golang-nuts
os.Getwd() works for me.

On 8 January 2011 18:21, Michael Thomas Lee <bakato...@gmail.com> wrote:
> Hello, is there any way of getting Go to recognize the current working
> path?
> os.Getwd() and os.Getenv("PWD") both points to the home folder, while
> ".", or "./" does not get recognized at all.

Michael Thomas Lee

unread,
Jan 8, 2011, 2:43:33 PM1/8/11
to golang-nuts
path, _ := os.Getwd()
image := sdl.LoadImage(path+"test.png")
image.SaveBMP(path+"dump.bmp")
texture := image.CreateTexture()

This is the part of the code that fails. os.Getwd() returns the home
directory instead of the current working directory where the
application is located.
Just using this code works on Windows and Linux, but not on Mac Os:

image := sdl.LoadImage("test.png")
image.SaveBMP("dump.bmp")
texture := image.CreateTexture()

Any hints?

On Jan 8, 8:09 pm, roger peppe <rogpe...@gmail.com> wrote:
> os.Getwd() works for me.
>

Roger Pau Monné

unread,
Jan 8, 2011, 2:54:01 PM1/8/11
to Michael Thomas Lee, golang-nuts
You should check the error value of os.Getwd():

path, err := os.Getwd()
if err != nil {
panic(err)
}

Anyway, os.Getwd() works fine on my machine (6g, 6l, Mac OS X 10.6.6),
just tested it now. What version of the compiler/OS are you using?

Also you can you try this simple program:

package main

import(
"fmt"
"os"
)

func main() {
fmt.Println(os.Getwd())
}

2011/1/8 Michael Thomas Lee <bakato...@gmail.com>:

Michael Thomas Lee

unread,
Jan 8, 2011, 3:21:01 PM1/8/11
to golang-nuts
There is no error value. The standalone program outputs the correct
path. I guess it must be a part of the code that somehow resets the
current working directory to the home folder. Adding the simple
program code to the full app, outputs the home folder again.

On Jan 8, 8:54 pm, Roger Pau Monné <roy...@gmail.com> wrote:
> You should check the error value of os.Getwd():
>
> path, err := os.Getwd()
> if err != nil {
>     panic(err)
>
> }
>
> Anyway, os.Getwd() works fine on my machine (6g, 6l, Mac OS X 10.6.6),
> just tested it now. What version of the compiler/OS are you using?
>
> Also you can you try this simple program:
>
> package main
>
> import(
>         "fmt"
>         "os"
>         )
>
> func main() {
>         fmt.Println(os.Getwd())
>
> }
>
> 2011/1/8 Michael Thomas Lee <bakatorre...@gmail.com>:

Gustavo Niemeyer

unread,
Jan 8, 2011, 4:13:27 PM1/8/11
to golang-nuts

Hi Michael,

The current working directory is the path your shell is currently at. If you want to obtain the path the executable is under, use the path of os.Args[0]

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter

Christian Himpel

unread,
Jan 9, 2011, 3:10:53 AM1/9/11
to golang-nuts
On Sat, Jan 8, 2011 at 10:13 PM, Gustavo Niemeyer <gus...@niemeyer.net> wrote:
> The current working directory is the path your shell is currently at. If you
> want to obtain the path the executable is under, use the path of os.Args[0]

Just to elaborate. You can use exec.LookPath[1] to find the executable
in your PATH variable, use path.Split[2] to extract the directory and
then use os.Chdir[3] to change the working directory.


cat > example.go << EOF
package main

import (
"exec"
"os"
"path"
)

func main() {
file, _ := exec.LookPath(os.Args[0]) // [1]
println("file:", file)
dir, _ := path.Split(file) // [2]
println("dir:", dir)
os.Chdir(dir) // [3]
wd, _ := os.Getwd()
println("wd:", wd)
}
EOF

[1]: http://golang.org/pkg/exec/#LookPath
[2]: http://golang.org/pkg/path/#Split
[3]: http://golang.org/pkg/os/#Chdir

cheers,
chressie

Michael Thomas Lee

unread,
Jan 9, 2011, 9:14:42 AM1/9/11
to golang-nuts
Thanks a lot. That worked out as it should.

Regards,
Michael

On Jan 9, 9:10 am, Christian Himpel <chres...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages