code Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/code/ Fri, 17 Apr 2020 18:19:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 code Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/code/ 32 32 159787465 Get ASCII code/value of any Alphabet or Number in Go (Golang) https://vikasboss.github.io/get-ascii-value-alphabet-go/ https://vikasboss.github.io/get-ascii-value-alphabet-go/#respond Fri, 17 Apr 2020 18:19:20 +0000 https://vikasboss.github.io/?p=2039 range over a string can give the ASCII of all the characters in the string. In below code we are printing the ASCII value of a lowercase alphabets, uppercase alphabets and numbers....

The post Get ASCII code/value of any Alphabet or Number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
range over a string can give the ASCII of all the characters in the string. In below code we are printing the ASCII value of a lowercase alphabets, uppercase alphabets and numbers.

package main

import "fmt"

func main() {
    lowercase := "abcdefghijklmnopqrstunwxyz"
    for _, c := range lowercase {
        fmt.Println(c)
    }

    uppercase := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for _, c := range uppercase {
        fmt.Println(c)
    }

    numbers := "0123456789"
    for _, n := range numbers {
        fmt.Println(n)
    }
}

Output:

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
110
119
120
121
122
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
48
49
50
51
52
53
54
55
56
57

The post Get ASCII code/value of any Alphabet or Number in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/get-ascii-value-alphabet-go/feed/ 0 2039
Return exit status code in Go (Golang) https://vikasboss.github.io/return-exit-status-code-go/ https://vikasboss.github.io/return-exit-status-code-go/#respond Mon, 13 Apr 2020 11:33:35 +0000 https://vikasboss.github.io/?p=1985 Overview ‘os’ package of golang provides an Exit function that can be used to exit the current program with a status code. Status code zero means success Non-zero status code means an...

The post Return exit status code in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
Overview

‘os’ package of golang provides an Exit function that can be used to exit the current program with a status code.

  • Status code zero means success
  • Non-zero status code means an error.

Once this function is called the program exits immediately. Even the deferred functions are not called.

Also to note that status code should be in the range [0, 125]

func Exit(code int)

Let’s see a working code

Code

package main

import (
    "fmt"
    "os"
)

func main() {
    success := true
    if success {
        fmt.Println("Success")
        os.Exit(0)
    } else {
        fmt.Println("Failure")
        os.Exit(1)
    }
}

Output

Try setting success to false to see a different output

Success
$ echo $?
0

The post Return exit status code in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/return-exit-status-code-go/feed/ 0 1985