How to get the directory of the currently running file

0 votes

In nodejs I use __dirname . What is the equivalent of this in Golang?

I have googled and found out this article http://andrewbrookins.com/tech/golang-get-directory-of-the-current-file/ . Where he uses below code

_, filename, _, _ := runtime.Caller(1)
f, err := os.Open(path.Join(path.Dir(filename), "data.csv"))

But is it the right way or idiomatic way to do in Golang?

May 5, 2020 in Java-Script by kartik
• 37,510 points
1,166 views

1 answer to this question.

0 votes

Hello @kartik,

This should do it:

import (
    "fmt"
    "log"
    "os"
    "path/filepath"
)

func main() {
    dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
    if err != nil {
            log.Fatal(err)
    }
    fmt.Println(dir)
}
answered May 6, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

0 votes
1 answer

How to get the file name from a full path using JavaScript?

Hello @kartik, Use this: var filename = fullPath.replace(/^.*[\\\/]/, '') This ...READ MORE

answered Sep 2, 2020 in Java-Script by Niroj
• 82,880 points
1,084 views
0 votes
1 answer

How to get the size of a JavaScript object?

Hello @kartik, Use this code: function roughSizeOfObject( object ) ...READ MORE

answered Sep 23, 2020 in Java-Script by Niroj
• 82,880 points
4,020 views
0 votes
1 answer

How to get the value of an input field using ReactJS?

Hello @kartik, Managed to get the input field ...READ MORE

answered Oct 8, 2020 in Java-Script by Niroj
• 82,880 points
13,711 views
0 votes
1 answer

How to to get the key of a key/value javascript object?

Hello @kartik, You would iterate inside the object ...READ MORE

answered Oct 9, 2020 in Java-Script by Niroj
• 82,880 points
649 views
0 votes
1 answer
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17, 2020 in Web Development by anonymous
• 37,510 points
733 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 770 views
0 votes
1 answer

What are the vulnerability related to PHP Form?

Hii, The $_SERVER["PHP_SELF"] variable can be used by ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,880 points
2,673 views
0 votes
3 answers
0 votes
1 answer

How to get “value” of file upload input field in jquery?

Hello @kartik, Yes ,you can read the value ...READ MORE

answered Jul 8, 2020 in Java-Script by Niroj
• 82,880 points
11,936 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP