Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:56:11+00:00 2026-05-29T22:56:11+00:00

This is a fairly niche problem, but I’m currently trying to write a conventions-based

  • 0

This is a fairly niche problem, but I’m currently trying to write a conventions-based settings storage library with golang. It would be a great API boon if I could programmatically determine the running package name that wants to store something (eg "github.net/author/projectname/pkg") calling my library function.

With Python a similar thing could be achieved with the inspect module, or even with __main__.__file__ and a look at the file system.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:56:12+00:00Added an answer on May 29, 2026 at 10:56 pm

    You can get similar information if you use the following functions:

    • runtime.Caller
    • runtime.FuncForPC

    The code may look like this:

    pc, file, line, ok := runtime.Caller(1)
    if !ok { /*failed*/ }
    println(pc, file, line, ok)
    
    f := runtime.FuncForPC(pc)
    if f == nil { /*failed*/ }
    println(f.Name())
    

    If I put the above code (with the 1st line changed into runtime.Caller(0)) into a (randomly chosen) Go library which I have installed in GOROOT, it prints:

    134626026 /tmp/go-build223663414/github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go -4585 true
    github.com/mattn/go-gtk/gtk.Init
    

    Or it prints:

    134515752 /home/user/go/src/github.com/mattn/go-gtk/example/event/event.go 12 true
    main.main
    

    The filename on the 1st line, and the 2nd line, seem to contain the information you are looking for.

    There are two problems:

    • It may give incorrect result if functions are automatically inlined by the compiler

    • For any function F defined in package main, the function name is just main.F. For example, if runtime.Caller(0) is called from main(), the function name is main.main even if the main() function is defined in a Go file found in GOROOT/src/github.com/mattn/go-gtk/.... In this case, the output from runtime.Caller is more useful than the output from runtime.FuncForPC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use this fairly standard line of code in my app: [[UIApplication
I know this is fairly subjective, but I'm diving into testing and learning about
I guess this is fairly simple for you but i cant wrap my head
This is a fairly trivial matter, but I'm curious to hear people's opinions on
This is a fairly common problem, it probably has a name, I just don't
I've got a, I think fairly easy question, but this is bugging me for
I'm fairly new to both Git and Ruby, but I'm wondering if this situation
Say I am storing addresses in a DB table, in this fairly common break
I'm sure this is fairly simple, however I have a major mental block on
I'd like to count and group rows by specific values. This seems fairly simple,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.