Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

How to use the IsExported function in Go

Ayesha Naeem

The IsExported() function in Go checks if a name is exported or not. A name is exported if it starts with a capital letter.

The program needs to import the go/ast module to use the IsExported() function.

Prototype

Parameters and return value

The IsExported() function accepts a string name and returns true if the name is exported; otherwise, it returns false.

Example

The following code demonstrates how to use the IsExported() function in Go.

package main

// import modules
import (
	"fmt"
	"math"
	"go/ast"
)

func main() {
  //Pi is exported from math module
	fmt.Println(math.Pi)
	x := ast.IsExported("Pi")
	y := ast.IsExported("pi")
	fmt.Println(x)
	fmt.Println(y)
}

The code above imports the math module, which exports Pi; therefore, it starts with a capital letter. The program reports true when Pi goes through the IsExported() function.

RELATED TAGS

golang

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring