Found 1082 Articles for Go Programming

Golang program to use different types of a collection

Akhil Sharma
Updated on 22-Feb-2023 13:50:27

768 Views

In Golang, a collection is a data structure that stores a number of elements, usually of the same type. The collection types in Go offers arrays, slices, maps, and channels. Slices are considered as dynamic arrays whose size can be changed whereas arrays have a fixed size. Channels give goroutines a method to communicate and synchronize their execution, whereas maps are used for key-value storage. Here, we will show execution of maps, slices and arrays using different ways as shown below in examples. Method 1: Using map, slice and array This method shows how to use arrays, slices, and maps ... Read More

Golang program to join two linked list

Akhil Sharma
Updated on 22-Feb-2023 11:12:10

293 Views

In Go, a linked list is a linear data structure that contains a node which further contains two fields one is data and the other one is next. The next contains the address of the next node in continuation. We will execute this program with the help of two methods. In the first method linked list will be used a series of nodes and in the second example different variables are used to execute the program. Method 1 − Using linked list as series of nodes A linked list is described by this program as a collection of Nodes, each ... Read More

Golang program to perform inorder tree traversal

Akhil Sharma
Updated on 21-Feb-2023 12:18:30

369 Views

In Go programming language, a tree is a frequently used data structure that resembles an upside-down tree or an inverted tree with parent-child relationships between the nodes. In a tree, each node has a value and zero to many nodes as offspring. The root node is the node without a parent, while the leaves are the nodes without children. Trees can be employed for a variety of tasks, including data storage, sorting, and searching in hierarchical structures. We will use two methods to perform inorder tree traversal. Syntax func make ([] type, size, capacity) The make function in go ... Read More

Golang program to convert the arraylist into string and vice-versa

Akhil Sharma
Updated on 21-Feb-2023 12:17:44

317 Views

In go we can use strings.Join to convert a arraylist into string. Here we will understand how we can use strings.Join to combine multiple texts from an array that are separated by commas and spaces also, strings. To create an array of strings, use the Split function on the string. Let’s see through the code and algorithm how its executed. Syntax func Join(s []string, sep string) string The join function is used to convert an array to string. This function is present in strings package. it takes two arguments first one is the array that we wish to convert ... Read More

Golang program to convert a linked list into an array and vice-versa

Akhil Sharma
Updated on 21-Feb-2023 12:16:29

255 Views

In the Go programming language, a linkedlist is a data structure made up of a series of nodes, each of which has a value and a reference (pointer) to the node after it. Since items can be added to or removed from a list without rearranging the entire data set, linked lists offer a dynamic and adaptable approach to store data. Using structs and pointers, linked lists can be implemented in Go whereas array is a fixed-size group of identical elements that may be accessed by their respective indexes, which are integers with a zero-based basis. An array's size is ... Read More

Golang program to make a file read-only

Akhil Sharma
Updated on 21-Feb-2023 12:15:32

1K+ Views

We can use chmod function and syscall package in Golang to make a file read-only file. To convert a file into read-only files, the chmod function set the file’s right to 044. The os.Chmod function is then used to modify the file's mode. In syscall package of Golang we are going to use os.stat function to obtain information about the file. Method 1: Using chmod function This program employs the Chmod function to modify a file's permissions using the os package. Syntax os.Chmod In Go, this function belongs to the os package. The main goal of this function is ... Read More

Golang program to write into a file

Akhil Sharma
Updated on 21-Feb-2023 12:14:20

3K+ Views

In go programming language we can use os.create and ioutil.WriteFile to write into a file. In Go, the OS can be used to represent a file. os package file type that offers ways to open, read from, write to, and manipulate files. Method 1: Using os.Create function We make use of the os.Create in this program to start a new file or, if one already exists, to open it. The file is deferred while a string is written using the WriteString function. Close makes ensuring that when the application ends, the file is correctly closed. Syntax Os.Create In ... Read More

Golang program to create a temporary file

Akhil Sharma
Updated on 21-Feb-2023 12:13:30

2K+ Views

In Go programming language we can use ioutil.TempFile function to create a temporary file. To create a new temporary file in the default temporary directory, this program uses the ioutil. TempFile function. The directory in which the file should be created is the first argument to TempFile, and the pattern to use for the file name is the second argument. The pattern in this instance is "temp-*.txt, " leading to a file name like "temp-123456789.txt". Syntax Ioutil.TempFile In Go programming language, this function is a part of ioutil package and is used to create a temporary file. A ... Read More

Golang program to create a new file

Akhil Sharma
Updated on 21-Feb-2023 12:12:31

3K+ Views

Golang has two internal function – os.create and ioutil.WriteFile to create a new file. The "file" is a disk-based file that a Go program may read from or write to. In Go, the OS can be used to represent a file. os package file type that offers ways to open, read from, write to, and manipulate files. Method 1: Using os.Create Command In this method, the os.Create function uses the specified name to create a new file or truncate an existing one. When the function has completed running, the file is closed using the defer command. Syntax Os.Create ... Read More

Golang program to convert file to byte array

Akhil Sharma
Updated on 21-Feb-2023 12:11:15

10K+ Views

In Go programming language we can use byte function and ioutil.ReadFile function to convert a file into byte array. The os package file type that offers ways to open, read from, write to, and manipulate files whereas array is a fixed-size group of identical elements that may be accessed by their respective indexes, which are integers with a zero-based basis. An array's size is predetermined at the moment of declaration and cannot be altered later on. When storing a set of data with a known number of elements, arrays in Go are handy, but they have some drawbacks, such as ... Read More

Advertisements