简体   繁体   中英

Is there a Golang library for parsing go sources and returning a stdlib's identifier list?

I'm looking for a library that should parse a go source code and returns the list of identifies related to the Go's standard library. For instance, after processing the following code:

package main

import (
    "os"
    "os/signal"
    "syscall"
)

func main() {
    c := make(chan os.Signal, 1)
    signal.Notify(c, syscall.SIGINT, syscall.SIGUSR2)
}

the output should resemble something like: os: Signal: 1 os/signal: Notify: 1 syscall: SIGINT: 1, SIGUSR2: 1

(I just randomly was looking over old stuff I touched and saw this question again. I think I read the question very differently this time.)

I think this question is based on the false premise that Go's standard library is communicating with the OS via C's stdlib as most other programs do. With the exception of the optional CGo library, Go binaries do not generally link to C libraries, either statically (via .a files) or dynamically, via a .so or .dll. Go's standard library communicates with the OS kernel via system calls implemented with interrupts and the like. There are no stdlib symbols because Go's standard library doesn't call C's stdlib.

Original answer from Oct-2018: I'm not sure what you want to do specifically, but generally a list of tokens isn't particularly useful. Generally you want to look at walking an AST (Abstract Syntax Tree) which gives you most of the same information within a more meaningful context. For this you probably want to look at the go/ast and go/parser standard library packages should give you most of what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM