Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with V Programming

You're reading from   Getting Started with V Programming An end-to-end guide to adopting the V language from basic variables and modules to advanced concurrency

Arrow left icon
Product type Paperback
Published in Dec 2021
Publisher Packt
ISBN-13 9781839213434
Length 408 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Navule Pavan Kumar Rao Navule Pavan Kumar Rao
Author Profile Icon Navule Pavan Kumar Rao
Navule Pavan Kumar Rao
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Section 1: Introduction to the V Programming Language
2. Chapter 1: Introduction to V Programming FREE CHAPTER 3. Chapter 2: Installing V Programming 4. Section 2: Basics of V Programming
5. Chapter 3: Variables, Constants, and Code Comments 6. Chapter 4: Primitive Data Types 7. Chapter 5: Arrays and Maps 8. Chapter 6: Conditionals and Iterative Statements 9. Chapter 7: Functions 10. Chapter 8: Structs 11. Chapter 9: Modules 12. Section 3: Advanced Concepts in V Programming
13. Chapter 10: Concurrency 14. Chapter 11: Channels – An Advanced Concurrency Pattern 15. Chapter 12: Testing 16. Chapter 13: Introduction to JSON and ORM 17. Chapter 14: Building a Microservice 18. Other Books You May Enjoy

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime