0

i want know if there`s a way to read a go and get the content from that file using go.

for example: I have a file called car.go and inside this file has a struct called Car, i want to read that struct Car properties.

  • https://pkg.go.dev/go/parser#ParseFile – tonys Jul 09 '22 at 02:44
  • Can you elaborate what you are trying to achive exactly ? Whether you're trying to parse go code for some reason or inspect type dynamically at runtime, it will raise different solutions. – davidriod Jul 09 '22 at 12:49

2 Answers2

0

Not sure whether I am getting it right, but if you are trying to use struct that is defined in other file, then you can import the struct in your current file.

Note - If both the files are inside a comman module, then you don't need to import, you can directly use all structs condition that their first letter is capital.

Refer this for better understanding of import - link

In case you want to read file of any extensions, you can refer this thread - Link

Hope this helps you!

Sachin Som
  • 1,005
  • 3
  • 8
0

You can parse go files with a go package. Take a look at this example: https://pkg.go.dev/go/ast#example-CommentMap

Tinkerer
  • 865
  • 7
  • 9