GoLang – Sample – helloWorld

Background

Let us go with a simple hellowWorld sample applications.

Exercise – helloWorld

Outline

  1. package
    • package main
      • Main Routine
  2. import
    • List packages to import
      • fmt
  3. Functions
    • main
      • Print messages using fmt.Println
        • print “hello world

Code


package main

import "fmt"

func main(){
    fmt.Println("hello world")
}

Run

Invoke


go run helloWorld_01.go

Output

Output – Image

Output – Text


>go run helloWorld_01.go
hello world

 

Exercise – helloWorldPersonalize

Outline

  1. package
    • package main
      • Main Routine
  2. import
    • List packages to import
      • fmt
      • os/user
      • strings
  3. Functions
    • stringIsEmpty
      • Determine if string is empty
        • Trim String
        • Checked trimmed strings length against 0
    •  displayHello
      • Access user object
        • Get user.Current
          • Place value in objUser
          • Place error in objError variable
        • If objError
          • Display error
          • Exit Function
      • Print messages using fmt.Println
        • print “hello world”
        • print name ( objUser.Name )
        • print username ( objUser.username )

Code


package main

import (
    "fmt"
    "os/user"
    "strings"
)

/*

    What is the best way to test for an empty string in Go?
    https://stackoverflow.com/questions/18594330/what-is-the-best-way-to-test-for-an-empty-string-in-go

*/
func stringIsEmpty(s string) bool {
    return len(strings.TrimSpace(s)) == 0
}

func displayHello(){

    /*
        Declare Variables
            objUser as pointer (*) to user.User
    */
    var objUser  *user.User
    var strFormattedName string
    var strBuffer string
    var objError  error
    
    /*
        Get current user by accessing the user object
        https://golang.org/pkg/os/user/
        Imported Module os/user
    */
    objUser, objError = user.Current()
    
    if objError!= nil {

        strBuffer = fmt.Sprintf(
                                    "Failed to properly access the %s module\n", 
                                    "user.Current")                     
    
        fmt.Println(strBuffer);
    
        panic(objError)
    }
    
    strFormattedName = "";
    
    /*
        If Name is indicated
    */
    if (stringIsEmpty(objUser.Name) == false) {
    
        strFormattedName = objUser.Name;
        
        if (objUser.Username != "") {
        
            strFormattedName = strFormattedName + " (" + objUser.Username + " )";
            
        }
        
        
    } else {
        
            if (stringIsEmpty(objUser.Username) == false) {
            
                if (strFormattedName == ""){
                
                    strFormattedName = objUser.Username;
                 
                }else
                {
        
                    strFormattedName = strFormattedName + " (" + objUser.Username + " )";
                }
                
            }
            
    }
    
    
    strBuffer = fmt.Sprintf("Hi %s\n" , strFormattedName);
    
    fmt.Println(strBuffer);

}


func main(){
	displayHello()
}

Run

Invoke


go run helloWorldPersonalize_01.go

Output

Output – Image

Output – Text


Hi LAB\dadeniji

Source Code Control

Github

 

  1. Repository
    • Golang/exercises/helloWorld
      Link

      • helloWorld.go
        Link
      • helloWorldPersonalize.go
        Link

 

References

  1. Go

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s