Go language: Using package name inside package scope (for Examples) -


I am currently writing exampleFuncs in my Go Test Script for example: package Hello import "test" func ExampleGetSymbol () {Data: GetSymbol ("AAPL") fmt.Println (Lane (data.Data)) // Output: 21}

Now, I get two benefits from this:

  1. This example is executed when I am running go test and
  2. This appears in the Godok document under func GetSymbol

    One thing bothers me And I wonder if I should have done anything about it. For the user trying to learn from this example, line

      data: = GetSymbol ("AAPL")   

    should really be

      Data: Hello = Hello Gate Simbol ("AAPL")   

    But since the package is in the same area as a package, I can not use it this way.

    So I think the disturbing version of my question will be: Is there a way to allow package.field notation inside the package?

    Thanks

    Hello Put it in the package, hello_test . You have permission for both Hello and Hello_Test packages in the same directory, and this allows you to create the way you are suggesting your examples (required) .

    Incidentally, this causes you to write for your test cases (at least for this file) only for public APIs. This is often a good thing but if you have to write in personal work , You can split your test into separate files, some in the Hello package and some hello_test .

    BTW, documentation for this is a little buried in the "Test package" section.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -