Golang: Create Application

Create a folder name hello

mkdir hello
cd hello

Initialize Golang module

go mod init my-hello

Create a folder (package) name english

mkdir english
cd english

Create a file inside the english folder name a.go

package english

import "fmt"

func Hello() {
  
  fmt.Println("Hello")
}

Go back to the root folder and create a file name x.go

package main

import "my-hello/english"

func main() {
  
  english.Hello()
}

This should output

-> % go run .
Hello

Create a folder (package) name french

mkdir french
cd french

Create a file inside the french folder name b.go

package french

import "fmt"

func Hello() {
  
  fmt.Println("Bonjour")
}

Now we go back to the root folder and edit x.go

package main

import (
  "my-hello/english"
  "my-hello/french"
)

func main() {
  
  english.Hello()
  french.Hello()
}

now we run it again

-> % go run .
Hello
Bonjour

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *