如何在Go中为parseFile编写测试

I have a parseFile function that given a file parsing for certain information and returns a struct. How do I write a test for this function? With Java, I could have a file in the test folder but I not sure how to best do it in Go.

Thanks and regards

You should be able to commit a file to the same folder as your test, and read in a relative path by way of os.Open("./path_to_file").

If the issue is that you're looking for a file in a specific location, I'd recommend parameterizing whichever method loads the information to accept different filepaths.

You can do exactly the same in Go, albeit it is handled a bit differently. Create a folder called "testdata" next to your *_test.go files.

This folder is by convention ignored by the go tools, as are all folders beginning with an underscore. This is why I name my resources directories for web applications _components instead of bower_components, for example.

Now, you can access said folder with a relative path from your tests, and it won't pollute your namespace or sth.