Book Image

Getting Started with V Programming

By : Navule Pavan Kumar Rao
4 (1)
Book Image

Getting Started with V Programming

4 (1)
By: Navule Pavan Kumar Rao

Overview of this book

A new language on the block, V comes with a promising set of features such as fast compilation and interoperability with other programming languages. This is the first book on the V programming language, packed with concise information and a walkthrough of all the features you need to know to get started with the language. The book begins by covering the fundamentals to help you learn about the basic features of V and the suite of built-in libraries available within the V ecosystem. You'll become familiar with primitive data types, declaring variables, arrays, and maps. In addition to basic programming, you'll develop a solid understanding of the building blocks of programming, including functions, structs, and modules in the V programming language. As you advance through the chapters, you'll learn how to implement concurrency in V Programming, and finally learn how to write test cases for functions. This book takes you through an end-to-end project that will guide you to build fast and maintainable RESTful microservices by leveraging the power of V and its built-in libraries. By the end of this V programming book, you'll be well-versed with the V programming language and be able to start writing your own programs and applications.
Table of Contents (19 chapters)
1
Section 1: Introduction to the V Programming Language
4
Section 2: Basics of V Programming
12
Section 3: Advanced Concepts in V Programming

What this book covers

Chapter 1, Introduction to V Programming, introduces the V programming language and its features. It also looks at the past, present, and future of V. Learn about the cross-compatibility of V and the list of operating systems V runs on.

Chapter 2, Installing V Programming, offers step-by-step details that will help you learn how to install the V programming language on the Windows and *nix OSes. You'll also learn how to ensure V is added to environment variables on Windows and symlinking on *nix OS, which will facilitate you accessing V from any directory.

Chapter 3, Variables, Constants, and Code Comments, introduces you to basic programming concepts such as how to define and work with variables and constants in V. This chapter also covers how to add single- and multi-line comments in V.

Chapter 4, Primitive Data Types, acquaints you with the world of primitive data types. You'll come to understand various primitive data types in V such as string, bool, rune, and other numeric types. You'll also learn how to work with these types. This chapter also introduces you to the various operators you can use on these data types such as relational, logical, arithmetic, bitwise, and shift operators. If you are an absolute beginner to the world of software programming, this chapter is highly recommended to get you hands-on with the concepts related to the primitive data types that are similar in all other programming languages.

Chapter 5, Arrays and Maps, covers different ways to declare arrays and how to initialize them using various properties available to define an array. You'll also see how to use in and << operators on arrays. We then walk through the details about working with fixed-size arrays and multi-dimensional arrays. You will also understand arrays by performing the most frequently performed operations on arrays, such as cloning, sorting, and filtering techniques.

You will also learn about maps that hold data in the form of key-value pairs. Maps in the V language are often referred to as dictionaries in other programming languages, such as C# and Python. You will learn how to work with maps, and understand various ways we can declare and initialize maps. You will also learn how to perform various operations on a map, such as retrieving the key-value pair given a key, and handling the retrieval of non-existent keys using an or block. Then you will see how to add, update, and delete key-value pairs from a map.

Chapter 6, Conditionals and Iterative Statements, looks in detail at conditionals and iterative statements in the V programming language. You'll learn about working with conditional blocks such as if, if-else, and chaining else-if along with the usage of goto statements supported by labels. You will also learn in depth about match block, which is used for use cases that involve pattern matching or conditional code branching. In the later parts of this chapter, we will look at the various types of operations performed on maps, arrays using for loops, and then you will see the different ways to write iterative statements using a for loop.

Chapter 7, Functions, introduces the various types of functions V lang offers. You will then be able to write basic functions, anonymous functions, and higher-order functions. This chapter will also guide you through the vast features that functions come equipped with, along with code examples for each feature.

Chapter 8, Structs, looks at how to write structs that involve struct fields having different access modifiers and also defining struct fields with default values. You will then learn the skill of defining and initializing structs. You will then have a fair understanding of how to add methods that belong to a struct and create and work with functions with structs as input arguments.

Chapter 9, Modules, first introduces the basic syntax to define and import modules. In the later sections, you will see how to create a simple project in V, and then learn how to create and import modules defined in our simple project. You will then learn how to create multiple files and work with those in a module and understand the access scope.

You will also learn about best practices that include the benefits of the initializer function and conditions to define the initializer function for a module. This chapter also covers problems that arise in V when we have cyclic imports while creating modules. In addition, you will understand member scopes and the accessibility of members, including constants, structs, and embedded structs across modules. By the end of this chapter, you will be well-versed in the concept of modules and how to leverage them while writing programs in V.

Chapter 10, Concurrency, looks at concurrency, which is a crucial topic when it comes to V. The main essence of V lies in the concurrency capabilities that it offers programmers. In this chapter, you will learn in depth about the concept of concurrency, along with detailed code examples. This chapter begins with an explanation of a simple real-life scenario, which is about performing daily morning routines. The chapter also looks at a comparison of the results of the tasks when they were performed sequentially, and the benefits when similar tasks are performed concurrently.

In addition to the more intuitive explanation on concurrency in this chapter, the chapter sets foundational knowledge on concepts such as time modules and thread types to get started with concurrent programming in V.

By the end of this chapter, you will be confident enough to write concurrent code in V using the go keyword and handling concurrent functions using thread types. You will be able to understand the benefit of writing concurrent code in contrast to sequential code. This chapter will also help you understand how to concurrently spawn void functions, functions that return values as well as anonymous functions. You will also learn how to share data between the main thread and the tasks that are spawned to run concurrently using locks.

Chapter 11, Channels – An Advanced Concurrency Pattern, covers channels, which is the advanced concurrency pattern in V. They solve the problem of explicitly handling data synchronization techniques among coroutines. In the context of concurrency, channels facilitate us to share data by establishing a communication channel between concurrent tasks. These concurrent tasks are often termed coroutines, which share the data by communicating through channels.

We can communicate between coroutines with the help of shared objects. But the problem with this approach is you need to take explicit care of concurrency synchronization techniques such as protecting the shared objects using locks such as the read-only rlock or the read/write lock to prevent data races. This is where channels in V come into the picture. Channels implicitly take care of all the aforementioned data synchronization techniques among coroutines, which otherwise are cumbersome to manage manually.

In this chapter, we begin by looking at the syntax to declare channels and then understand different types of channels, such as buffered and unbuffered channels. We then learn about the properties of channels. Later, we learn about various methods available on a channel.

Chapter 12, Testing, covers how to write test cases for functions in V. This chapter also covers what the minimum prerequisites are for your code and file structure in order for V to identify and run them as tests.

Chapter 13, Introduction to JSON and ORM, explains how to work with the built-in libraries in V, namely json and orm. When building web services such as RESTful APIs, it is essential to understand the content type that you are exchanging with the clients or other RESTful APIs. The JavaScript Object Notation (JSON) format has become the new normal for modern applications to exchange data. This chapter will touch upon a brief introduction to JSON and how to work with JSON in V.

When building data-driven applications, Object Relational Mappers (ORMs) become a crucial part to establish communication between the world of objects and the world of relational databases. This chapter also introduces the built-in library orm, which ships along with the V installer.

Chapter 14, Building a Microservice, looks at how to build a simple microservice in V using a RESTful approach. To achieve the implementation, you will be using the built-in libraries vweb and orm. You will also leverage the power of SQLite, by installing it as a third-party library. With the help of the SQLite library, you will learn how to establish a database connection and interact with it. You will use the JSON data format as a form of communication.