Problem opening a file with a variable as part of the path as well as reading input from command line

252 views
Skip to first unread message

vendion

unread,
Mar 27, 2010, 10:19:39 AM3/27/10
to golang-nuts
I am trying to open a file using a variable as part of the path to the
file, using os.Getevn("HOME") for the users home directory. When I
try to open the file using a C style method (sorry for better lack of
words here):

Config, err := os.Open(Home, "/.backup-light/backup-lightrc", 755)

it gives me errors during compile time

backup-light.go:120: cannot convert "/.backup-light/backup-lightrc" to
type int
backup-light.go:120: cannot use "/.backup-light/backup-lightrc" (type
string) as type int in function argument

As for reading input from the command line I used the example in the
"Go for Dummies" book that is being done by a couple of people here
and they suggest something like this:

func main() {
fmt.Println("Write something:")

in := bufio.NewReader(os.stdin)
result, _ := in.ReadString('\n')
text := strings.TrimSpace(result)

fmt.Println(text)

}

When I try to compile with that I get to following errors:

backup-light.go:124: cannot refer to unexported name os.stdin
backup-light.go:124: undefined: os.stdin
backup-light.go:125: undefined: in


Jessta

unread,
Mar 27, 2010, 10:28:15 AM3/27/10
to vendion, golang-nuts
On Sun, Mar 28, 2010 at 1:19 AM, vendion <ven...@gmail.com> wrote:
> Config, err := os.Open(Home, "/.backup-light/backup-lightrc", 755)
I suggest you look that the definition of os.Open again
func Open(name string, flag int, perm int) (file *File, err Error)
http://golang.org/pkg/os/#File.Open

> When I try to compile with that I get to following errors:
>
> backup-light.go:124: cannot refer to unexported name os.stdin
> backup-light.go:124: undefined: os.stdin
> backup-light.go:125: undefined: in

In go, lowercase functions and variables are private to the package they are in.
You're probably looking for os.Stdin <-- note the uppercase 'S'

- jessta

--
=====================
http://jessta.id.au

Dean Prichard

unread,
Mar 27, 2010, 11:04:03 AM3/27/10
to golang-nuts
I think you want something like:

Home := os.Getenv("HOME")
Config, err := os.Open(Home + "/.backup-light/backup-lightrc",
os.O_RDONLY, 755)

Adam Jimerson

unread,
Mar 27, 2010, 11:05:52 PM3/27/10
to golan...@googlegroups.com
On Sat, Mar 27, 2010 at 10:28 AM, Jessta <jes...@gmail.com> wrote:
On Sun, Mar 28, 2010 at 1:19 AM, vendion <ven...@gmail.com> wrote:
> Config, err := os.Open(Home, "/.backup-light/backup-lightrc", 755)
I suggest you look that the definition of os.Open again
func Open(name string, flag int, perm int) (file *File, err Error)
http://golang.org/pkg/os/#File.Open

Even with the one line explanation I am still confused about what it is looking for, I have tried pretty much every combination I could think of still no go.

Daniel Smith

unread,
Mar 27, 2010, 11:26:16 PM3/27/10
to Adam Jimerson, golan...@googlegroups.com
Is this what you're looking for?

Config, err := os.Open(Home + "/.backup-light/backup-lightrc", os.O_RDWR, 0755)


To unsubscribe from this group, send email to golang-nuts+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.



--
Daniel Smith
http://www.schaumburggoclub.org/
Reply all
Reply to author
Forward
0 new messages