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

DannyB

I am currently writing ExampleFuncs in my Go test scripts. For example:

package hello

import "testing"

func ExampleGetSymbol() {
    data := GetSymbol("AAPL")
    fmt.Println(len(data.Data))
    // Output: 21
}

Now, this gives me two benefits:

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

One thing bothers me and I am wondering if there is anything I should do about it. For the user that's trying to learn from this example, the line

data := GetSymbol("AAPL")

should actually be

data := hello.GetSymbol("AAPL")

but since the test is in the same scope as the package, I cannot use it like this.

So I guess the distilled version of my question would be:
Is there a way to allow package.field notation inside the package scope?

Thanks in advance

Rob Napier

Rather than putting this in package hello, put it in package hello_test. You're allowed to have both hello and hello_test packages in the same directory, and it allows (requires) you to create your examples the way you're suggesting.

Incidentally, this also causes you to write your test cases (at least for this file) only to the public API. This is often a good thing. But if you need to write to private functions, you can split your tests into separate files, some in the hello package and some in hello_test.

BTW, documentation for this is slightly buried. You can find it in the "Test packages" section of the go cmd documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Proper package naming for testing with the Go language

From Java

How to remove an installed package using go modules

From Dev

Get All Activities by using package Name

From Dev

Disambiguate package name from local var in Go

From Dev

How is package scope achieved?

From Dev

Go language package structure

From Dev

Package Variable in Perl not in "scope"?

From Dev

Open app with package name using ADB

From Dev

perl package inside a package

From Dev

How to get name of current package in go?

From Dev

Using the go/ast package

From Dev

How to use the parallel package inside another package, using devtools?

From Dev

Using the go-fuse package in Golang

From Dev

Access package comment from inside Go program?

From Dev

Creating short examples for CRAN package

From Dev

How to access meteor package name inside package?

From Dev

Renaming Android package name using Android Studio

From Dev

who's using my Go package

From Dev

go not running program with name package_test.go

From Dev

Load package contents without using their name afterwards

From Dev

Find package name using terminal

From Dev

Lombok builder package scope

From Dev

Find package name using terminal

From Dev

Rename package name inside Android project

From Dev

Package Variable in Perl not in "scope"?

From Dev

Launch app using package name

From Dev

How to access meteor package name inside package?

From Dev

How to make package inside master package in Android project using Eclipse?

From Dev

package object definitions are not in scope