Go Programming Variables, Data Types and Built-in Functions

Go Programming Variables, Types and Built in Functions


In this article, we will see the variables and data types and Built-in Functions. The basic Data types, variables, keywords and control structures of our new language. Go has a C programming like feel when it comes to its syntax.

It does not require a semicolon after two (or more) statements on one line in your code. They are only required if you want to combine multiple statements on a single line

Go is much easier and different from other languages in that the data type of a variable specified after the variable name. So not declare int a, but a int. When declaring a variable is assigned the natural value null for the type. It means that after var a int, a has a value of 0. With var b string, b assign the zero string, which is.

Assigning and declaring in Go is a two-step process, but they may be combined. Compare the following example of code which has the same effect.

here, we use the var keyword to define a variable and then assign a value to it.

var a int
var b bool

The following code uses: = to do this in one step. In that code, the variable type is deduced from the value. So the value of 15 indicates an int, a value of false tells Go that the type should be bool.

b := false
a := 15

a = 15
b = false

If we have multiple var value declarations may also be grouped import and const also allow this. use the following parentheses:

var (x int b bool)

We want to declare Multiple values of the same data type on a single line: var a, b int makes a and b both int variables. we can also pass the parallel assignment: a, b := 20, 16

If we want to declare unused variables then it will be showing compiler error. The following example generates this error: a declared and not used

package main
func main()
var a int

Boolean types

The boolean data type represents a set of boolean values. It predeclared constants values are true and false. The boolean type is bool.

func d() bool {
    var e bool
    return e

if d() {

Numerical types

Go has well-known types such as int. This data type has the appropriate length for your machine, the int is either 32 or 64 bits, no other values are defined. Same goes for uint.

If you want to be clear about the length. so you can have that too with int32, uint32. The list of signed and unsigned integers is int8, int16, int32, int64 and byte, uint8, uint16, uint32, uint64. the byte is an alias for uint8. with floating-point values, there is float32 and float64. A 64-bit integer or floating-point values are always 64 bit, also on 32-bit architectures.

package main
func main()
var a int //Generic integer type

var b int32 //32 bits integer type
a = 15 6
b = a + a //llegal mixing of these types 7
b = b + 5 //5 is a constant, so this is OK

we cannot use a + a (type int) as type int32 in assignment The assigned values may be denoted using octal, hexadecimal or the scientific notation: 077, 0xFF, 1e3 or 6.022e23 are all valid.


The value of a constant should be known at compilation time. Hence it cannot be reassigned to a value return by the function call since the function call takes place at run time. and it will fail with compilation error cannot assign.


const (
a = iota
b = iota

You can also explicitly type a constant if you need that:

const (
a = 0 //Is an int now
b string = “0”


The string data type provides an organized way to define a string is as simple as:

a: = “Hello World! ”

In the GoLang Strings is a sequence of UTF-8 characters enclosed in double-quotes (”). and the single quote (’) define one character which is not a string in Go.

Once we assign the value to a variable then string can not be changed: strings are immutable in Go. For people coming from C background, the following is not legal in Go:

var s string = “hello”

s[0] = ‘c’

If Change the first char. to ’c’, this is an error To do this in Go you will need the following:

s := “hello”
c := []rune(s)
c[0] = ‘c’
s2 := string(c)
fmt.Printf(“%s\n”, s2)

Multi-line strings

Due to the insertion of semicolons of two strings, you need to be careful with using multi-line strings. If you write like this:

s := “First String”
+ “Second String”

This is transformed into:

s := “First String”;
+ “Second String”;

Which is not valid syntax, you need to write:

s := “First String” + “Second String”

the number of following functions is predefined in Go, so, you don’t have to include any package to get access to them, because The built-in functions are documented in the built-in pseudo-package.

close len new
append panic print
complex imag delete
cap make copy
recover println real

1. close
It is used in channel communication. a sender can close to the channel and it indicates that no more values we’ll be sent. So, Receivers can test whether a channel has been closed by assigning a second value to the receive expression: after v, ok: = <-ch

2. delete
The delete built-in function, It is used for the element with the specified key (m[key]) from the map.

func delete(m map[Type]Type1, key Type)

3. len and cap
len is used for returning the length of strings and the length of slices and arrays. See section “Arrays, slices and maps” for the details of slices and arrays and the functioning capacity.

func len(v Type) int —new

It is a built-in function for allocating memory for user-defined data types.

func new(Type) *Type

The first argument is a type of function, not a value, and the value returned is a pointer to a newly allocated zero value of that type.

3. make
It is used for allocates and initializes an object of type (maps, slices, and channels). See

func make(t Type, size …IntegerType) Type

4. copy
It is a built-in function for copying slices from the source into a destination.

func copy(dst, src []Type) int

5. append
The append function is used for appends elements to the end of a slice.

func append(slice []Type, elems …Type) []Type

6. panic and recover
Both are used for an exception mechanism. It stops normal execution of the current goroutine.

func panic(v interface{})

7. print
It is built-in function formats and its arguments in an implementation-specific way and writes the result to standard error. the mainly used for debugging.

func print(args …Type)

8. Println
Println is useful for bootstrapping and debugging with the spaces are always added between arguments and a newline is appended.

func println(args …Type)

9. complex, real and imag
All deal with complex numbers. Apart from the simple example we gave, we will not further explain complex numbers.

type ComplexType complex64
func real(c ComplexType) FloatType
func imag(c ComplexType) FloatType

Leave a Reply

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