-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow users to list methods of struct/interface #2249
Comments
In the meantime, the
|
What about a |
I'm also interested. And I have found myself wanting this feature on more than one occasion. Specifically: Proposal:Add ability to list the public API of any variable/type and also the public api of any import. Problem statement:Whenever you are debugging using delve you want to know a number of things; For (a) we have Usage:Let's say you have a Go file like package main
import (
"compress/flate"
"net/http"
"time"
)
func main() {
_ = flate.BestSpeed
tomorrow := time.Hour * 24
req, _ := http.NewRequest("GET", "https://google.com", nil)
} You can then use the (dlv) ptype tomorrow
(dlv) ptype req
(dlv) ptype "compress/flate" And when you do so, the public API of the requested variables/types or package import will be printed. (dlv) ptype tomorrow
[
NAME: time.Duration
KIND: int64
SIGNATURE: [time.Duration]
FIELDS: []
METHODS: [
Hours func(time.Duration) float64
Microseconds func(time.Duration) int64
Milliseconds func(time.Duration) int64
Minutes func(time.Duration) float64
Nanoseconds func(time.Duration) int64
Round func(time.Duration, time.Duration) time.Duration
Seconds func(time.Duration) float64
String func(time.Duration) string
Truncate func(time.Duration, time.Duration) time.Duration
]
] (dlv) ptype req
[
NAME: net/http.Request
KIND: struct
SIGNATURE: [*http.Request http.Request]
FIELDS: [
Method string
URL *url.URL
Proto string
ProtoMajor int
ProtoMinor int
... <truncated> ...
]
METHODS: [
AddCookie func(*http.Request, *http.Cookie)
BasicAuth func(*http.Request) (string, string, bool)
Clone func(*http.Request, context.Context) *http.Request
... <truncated> ...
]
] (dlv) ptype "compress/flate"
[
NAME: compress/flate
CONSTANTS: [
BestCompression untyped int
BestSpeed untyped int
... <truncated> ...
]
VARIABLES: []
FUNCTIONS: [
NewReader(r io.Reader) io.ReadCloser
NewReaderDict(r io.Reader, dict []byte) io.ReadCloser
... <truncated> ...
]
TYPES: [
... <truncated> ...
Writer struct
(*Writer) Close() error
(*Writer) Flush() error
(*Writer) Reset(dst io.Writer)
(*Writer) Write(data []byte) (n int, err error)
CorruptInputError int64
(CorruptInputError) Error() string]
] Precedent:The struct complex {double real; double imag;} v;
(gdb) whatis v
type = struct complex
(gdb) ptype v
type = struct complex {
double real;
double imag;
} Implementation:The implementation would use the This proposal can be broken into two parts: I can avail any other information required to help people be able to evaluate this proposal. Thanks, |
I don't know whether to make my proposal an independent issue? |
Also a bit related to: #55 |
@komuw perhaps the new functionality could be a flag passed to
|
@icholy I would also be fine with that idea. |
good idea, I found this issue for almost the same purpose, to print a complex data structure without the souce code |
I need this feature, and I am trying to implement this. Now I have a problem to solve. Following is my testing: $ dlv debug main.go
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x10cb87b for main.main() ./main.go:17
(dlv) c
> main.main() ./main.go:17 (hits goroutine(1):1 total:1) (PC: 0x10cb87b)
12: def int
13: }
14:
15: var _ Student = Student{}
16:
=> 17: func main() {
18: ss := Student{Name: "xxxx"}
19: fmt.Println(ss)
20: }
(dlv) types Student
*main.Student
main.Student
(dlv) ptype main.Student
struct main.Student <nil>
(dlv) ptype int
int <nil>
(dlv) main.Student is defined and printed by I just use I expect I think I can do a $ dlv debug main.go
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x10cb87b for main.main() ./main.go:17
(dlv) ptype main.Student
struct main.Student
fields
0 Name string
1 Age int
2 Sex int
3 Address string
4 School string
5 abc int
6 def int Problem is
todo:
Next, I want to see how This modification is here: https://github.com/hitzhangjie/delve/tree/feat/ptype. using method1 mentioned above, now I can list the method's name (without parameter list). (dlv) ptype main.Student
struct main.Student
fields
0 Name string
1 Age int
2 Sex int
3 Address string
4 School string
5 abc int
6 def int
methods
String
Sing
Learn
<nil> After writing some code about DWARF, now it can list the parameter list (receiverType var is dropped) and return list: $ dlv debug main.go
Type 'help' for list of commands.
(dlv) ptype main.Student
struct main.Student
fields
0 Name string
1 Age int
2 Sex int
3 Address string
4 School string
5 abc int
6 def int
methods
func Learn(a int,b int) (~r2 error)
<nil> |
I had given this a shot one year ago and got it somehow working as a proof of concept with bad code.
when you do so, you get output like: (dlv) whatis -v f
===printVar:===
struct main.Fire {
fields:
HH main.Health
Age int64
Name string
Dist main.Distance
Day time.Time
methods:
Hello func(i int) string
MethodTwo func() int64
}
===printVar:===
main.Fire whatis -v hReq
===printVar:===
struct *net/http.Request {
fields:
Method string
URL *net/url.URL
Proto string
....
TLS *crypto/tls.ConnectionState
Cancel <-chan struct {}
Response *net/http.Response
methods:
BasicAuth func() (username string, password string, ok bool)
SetBasicAuth func(username string, password string)
...
Clone func(ctx context.Context) *net/http.Request
PostFormValue func(key string) string
Write func(w io.Writer) error
ParseMultipartForm func(maxMemory int64) error
Cookies func() []*net/http.Cookie
}
===printVar:===
*net/http.Request (dlv) whatis -v MyFn
===printVar:===
func(int, string, *uint64) void {
methods:
MethodOnFunc func() int64
}
===printVar:===
main.fn I don't have time to take it further than I did, so I'm just sharing whatever I have. |
@komuw Thanks for your sharing, I'm willing to learn your code later...and contribute this. |
It would be nice for developers to list all methods of a struct/interface in addition to its fields in dlv like python
dir(object)
.PS: though this expr doc says that dlv shows the concrete type of a interface, I also want to list all methods of a interface.
Additional information:
dlv version
)?The text was updated successfully, but these errors were encountered: