Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

How to call a function from another package in Go (Golang)

Posted on April 4, 2023April 4, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

The function in another package must start with a capital letter so that it is public outside its package. If the function name starts with lowercase then it will not be visible outside its package. 
To use a function outside its package we need to import the package first which contains that function. 

Program

Here is the code for the same

go.mod

module sample.com/learn
go 1.16

hello/hello.go

package hello
import "fmt"
func SayHello() {
fmt.Println("Hello")
}

main.go

package main
import "sample.com/learn/hello"
func main() {
hello.SayHello()
}

Output

Hello

In this program, we first import the hello package from the main package as below. 

import "sample.com/learn/hello"

Then we call the SayHello function as below from the main package

hello.SayHello()

This works because SayHello function is uppercase. 

Change the function to lower case and run this program. It gives the below compilation error

cannot refer to unexported name hello.sayHello

Also check out our Golang comprehensive tutorial Series – Golang Comprehensive Tutorial

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP