Golang builtin Package

Golang | builtin Package: Here, we are going to learn about the Golang builtin package, its constants, variables, and functions with examples.

builtin - Go Packages

In Go language, the builtin package provides documentation for Go's predeclared identifiers. The items documented here are not actually in package builtin but their descriptions here allow godoc to present documentation for the language's special identifiers.

To use these builtin package constants, variables, and function – there is no need to import any package.

Golang builtin Package Constants, Variables, and Functions

The following are the built-in constants, variables, and functions,

Constants

Constants Description
true, false The true and false are the two untyped boolean values, these are boolean constants.
iota It is a untyped int type of value that is a predeclared identifier (represents the untyped integer ordinal number of the current const specification in a (usually parenthesized) const declaration). It is zero-indexed.

Variables

Variables Description
nil It is a predeclared identifier representing the zero value for many types such as a pointer, channel, func, interface, map, or slice type.

Functions

Functions Descriptions
append() It is used to append elements to the end of a slice and returns an updated slice.
cap() It is used to get the capacity of the given slice.
close() It is used to close a channel, and the channel must be either bidirectional or send-only and should be executed only by the sender, never the receiver, and has the effect of shutting down the channel after the last sent value is received.
complex() It is used to construct a complex value from two floating-point values. The real and imaginary parts of the complex value must be of the same size (either float32 or float64), and the return value will be the corresponding complex type.
copy() It is used to copy the elements from a source slice into a destination slice and returns the number of copied elements copied.
delete() It is used to delete the element with the specified key from the map. If the map is nil or an empty map, delete is a no-op.
imag() It is used to get the imaginary part of the given complex number.
len() It is used to get the length of the given parameter, according to its type.
make() It is used to allocate and initializes an object of type slice, map, or chan (only). The return type of the make() function is the same as the type of its argument, not a pointer to it.
new() It is used to allocate memory. The first argument is a type, not a value, and the value returned is a pointer to a newly allocated zero value of that type.
panic() It is used to stop the normal execution of the current goroutine. When a function calls panic, the normal execution of the function stops immediately.
print() It is used to format the given arguments in an implementation-specific way and writes the result to standard error. The print() function is useful for bootstrapping and debugging purposes; it is not guaranteed to stay in the language.
println() It is used to format the given arguments in an implementation-specific way and writes the result to standard error. The whitespaces are always added between arguments and a newline is appended at the end of the line. The println() function is useful for bootstrapping and debugging purposes; it is not guaranteed to stay in the language.
real() It is used to get the real part of the given complex number. The return value will be floating point type corresponding to the type of the given complex number.
recover() is used to manage the behavior of a panicking goroutine. The recover() function is called inside a deferred function (but not any function called by it) to stop the panicking sequence by restoring normal execution and retrieving the error value passed to the call of panic. If the recover() function is called outside the deferred function it will not stop a panicking sequence. In this case, or when the goroutine is not panicking, or if the argument supplied to panic was nil, recover returns nil. Thus, the return value from recover reports whether the goroutine is panicking.

Example of append() function

// Golang program to demonstrate the
// example of append() function

package main

import (
	"fmt"
)

func main() {
	// Creating int and string slices
	s1 := []int{10, 20, 30}
	s2 := []string{"Hello", "World"}

	// Creating slices to append
	slice1 := []int{40, 50}
	slice2 := []string{"How're you?", "Boys"}

	// Printing types and values of slices
	fmt.Printf("%T, %v\n", s1, s1)
	fmt.Printf("%T, %q\n", s2, s2)

	// Appending the slices
	s1 = append(s1, slice1...)
	s2 = append(s2, slice2...)

	// After appending,
	// Printing types and values of slices
	fmt.Println("After appending...")
	fmt.Printf("%T, %v\n", s1, s1)
	fmt.Printf("%T, %q\n", s2, s2)
}

Output

[]int, [10 20 30]
[]string, ["Hello" "World"]
After appending...
[]int, [10 20 30 40 50]
[]string, ["Hello" "World" "How're you?" "Boys"]

Example of cap() function

// Golang program to demonstrate the
// example of cap() function

package main

import (
	"fmt"
)

func main() {
	// Creating int and string slices
	s1 := []int{10, 20, 30}
	s2 := []string{"Hello", "World"}

	// Printing types and values of slices
	fmt.Printf("%T, %v\n", s1, s1)
	fmt.Printf("%T, %q\n", s2, s2)

	// Printing the capacity
	fmt.Println("Capacity of s1:", cap(s1))
	fmt.Println("Capacity of s2:", cap(s2))

	// Appending some elements
	s1 = append(s1, 40, 50)
	s2 = append(s2, "How are you?", "Boys")

	// After appending,
	// Printing types and values of slices
	fmt.Println("After appending...")
	fmt.Printf("%T, %v\n", s1, s1)
	fmt.Printf("%T, %q\n", s2, s2)

	// Printing the capacity
	fmt.Println("Capacity of s1:", cap(s1))
	fmt.Println("Capacity of s2:", cap(s2))
}

Output

[]int, [10 20 30]
[]string, ["Hello" "World"]
Capacity of s1: 3
Capacity of s2: 2
After appending...
[]int, [10 20 30 40 50]
[]string, ["Hello" "World" "How are you?" "Boys"]
Capacity of s1: 6
Capacity of s2: 4

Example of complex() function

// Golang program to demonstrate the
// example of complex() function

package main

import (
	"fmt"
)

func main() {
	// Declaring & assigning float32, float64
	var a, b float32
	var c, d float64

	a = 10.56
	b = 25.50
	c = 1234.5678
	d = -165727.29982

	// Constructing complex values
	c1 := complex(a, b)
	c2 := complex(c, d)

	// Printing the types and values
	fmt.Printf("a : %T, %v\n", a, a)
	fmt.Printf("b : %T, %v\n", b, b)
	fmt.Printf("c1: %T, %v\n", c1, c1)
	fmt.Println()

	fmt.Printf("c : %T, %v\n", c, c)
	fmt.Printf("d : %T, %v\n", d, d)
	fmt.Printf("c2: %T, %v\n", c2, c2)
}

Output

a : float32, 10.56
b : float32, 25.5
c1: complex64, (10.56+25.5i)

c : float64, 1234.5678
d : float64, -165727.29982
c2: complex128, (1234.5678-165727.29982i)



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.