- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to create a module with constant
In the Go programming language, a module is a collection of packages and it helps to manage them and their dependencies. In this article, we will create a module with constant using two examples. In the first example, we will create a constant string that will be returned by the function and that function will be called from the main of another module. In the second example, a function will be created to return the string and print when called by the main of another module.
Algorithm
Import the required packages in the program
Create a constant string in a package
Call the package in another package and print the constant string
The print statement is executed using Println function from the fmt package
Example 1
In this example, we will create a package named mymodule which will be imported in another module. We will create a constant string which means it cannot be changed, then a function sayHello will be created that will return the constant string created to the function. When this package will be imported in the main the function will be called and the const string will be printed using the Println function from the fmt package.
//Golang program to create a module with constant package mymodule const Greetingalexa = "Hello, alexa!" //create string using const keyword func SayHello() string { return Greetingalexa //return the string to the function } package main import ( "fmt" "path/to/mymodule" //import the previous module in this module ) //Main function to execute the program func main() { fmt.Println(mymodule.SayHello()) //call the function and print the statement }
Output
Hello, alexa!
Example 2
In this illustration, a package mymodule will be created in the program and a function Greetingalexa will be created in which the string will be directly returned to the function. Here, the function will serve as the constant, it will be called from another module and the output will be printed on the console using Println function from the fmt package.
//Golang program to create a module with constant package mymodule func Greetingalexa() string { //create a function and return the string return "Hello, alexa!" } package main import ( "fmt" "path/to/mymodule" //import the previous module in this module ) //Main function to execute the program func main() { fmt.Println(mymodule.Greetingalexa()) //call the function and print the statement }
Output
Hello,alexa!
Conclusion
We executed and compiled the program of creating a module with constant using two examples. In the first example, we created a constant string, then returned and printed it. In the second example we created a function which serves as a constant, the string is returned to the function and printed on the console in the main function.