Programming Articles - Page 461 of 3363

Golang program to create a new file

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

7K+ 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

17K+ 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

Golang program to add elements to a linkedlist

Akhil Sharma
Updated on 21-Feb-2023 12:09:50

2K+ Views

In Golang, we can use node struct and linkedlist struct to add elements to a linkedlist. A linkedlist is a data structure made up of a series of nodes, each of which includes an element and a reference to the node after it in the sequence. It is a linear data structure with pointers connecting the items, and from the first node (head) to the last node, each node can be visited (tail). As opposed to arrays, which require all elements to be shifted, linked lists just require altering the pointers of the adjacent nodes, making them helpful in situations ... Read More

Golang program to implement graph data structure

Akhil Sharma
Updated on 21-Feb-2023 12:05:39

2K+ Views

In Go programming language, a graph is a type of data structure made up of a limited number of nodes (also known as vertices) and a set of connecting edges. Relationships between several entities can be depicted on a graph. It can be represented by employing different data structures, such as an adjacency matrix or an adjacency list. The particular use case and the needs of the application will determine the data structure to be used. A graph can also be implemented in Go by using a library or package like go-graph. We will use two methods here to implement ... Read More

Python Program to sort a tuple of custom objects by properties

Harshit Sachan
Updated on 20-Feb-2023 17:51:14

458 Views

Let us now dive into what a custom object in python is. We know that python provides us with a lot of pre-defined datatypes which can be used to store various types of data. But there are times where we have to define custom datatypes that can store data that cannot be represented by built-in datatypes. For this purpose, python provides us “classes”, these classes help us customize and create that is fit for our needs. A class can have data and function to implement some behavior. An example of implementing a class in python is as follows − class ... Read More

Python Program to Replace Elements in a Tuple

Harshit Sachan
Updated on 20-Feb-2023 15:57:46

5K+ Views

In this article, you will find out how to use Python to find the first and last elements of a tuple in this tutorial. The quick response is that you can access the first and last elements of the tuple in Python using the index operator([]). The methods described here can be used to obtain your single tuple element. Before diving in to the solution let us understand tuple in python. What is tuple in Python? A tuple is one of Python's four built-in data types for storing data collections. The other three are list, set, and dictionary, each ... Read More

Python Program to print key value pairs in a dictionary

Harshit Sachan
Updated on 20-Feb-2023 15:56:41

6K+ Views

A dictionary in python is a data structure which stores a collection of key value pairs. Unlike other data structures a dictionary contains 2 value at a particular place. A dictionary is a collection which is ordered, mutable and does not allow duplicate elements. A dictionary can be created by placing a sequence of elements within curly { } brackets , separated by ‘comma’ ( , ) . Dictionary holds pairs of values, one being the key and the other corresponding pair element being its value. Values in a dictionary can be of any data type and ... Read More

Golang program to access elements from a linked list

Akhil Sharma
Updated on 20-Feb-2023 16:17:00

463 Views

In Go Programming language, a linked list is a data structure which contains a node that further contains two values, the data and the next, where next points to the next node in the list. We will use two methods in this program to access elements from a linked list. In the first example iteration will be used and in the second example a variable current will be used to access the elements. Method 1: Using Iteration This program builds a linked list with three members and iterates through it to access and output each element's value. The output ... Read More

Python Program to search an element in a tuple

Harshit Sachan
Updated on 20-Feb-2023 15:55:54

7K+ Views

In Python, searching for elements within a data structure is a common task and different types of data structures should aim to provide efficient methods for searching. The problem of searching involves finding a specific element within a container and returning a value if it is not found. One data structure that can be used for this task is a tuple, which stores a collection of different types of data in a single variable. These items can be accessed by their index and Python offers various methods to work with them. Tuples are immutable, meaning that once created, they cannot ... Read More

Python Program to find the largest element in a tuple

Harshit Sachan
Updated on 20-Feb-2023 15:54:55

1K+ Views

One of the most common problems in computer science is the problem of searching. To check whether a given element exists in a variable is important and sometimes the item we must search for, can be the maximum, minimum, most frequent etc. in this article we will see how we can find the largest element in a tuple. We know that tuple is a pre-defined datatype that stores heterogenous data. It is sort of a container that holds several items inside it. We can define a tuple in python using the round brackets enclosing the data that we ... Read More

Advertisements