Getting Started with V Programming

Book description

Learn a new statically compiled programming language to build maintainable and fast software with the help of this comprehensive guide to V programming

Key Features

  • Explore the features of the V programming language step by step with this beginner's guide
  • Gain strong foundational knowledge of core programming concepts such as modules, functions, and structs
  • Learn how to write super-fast programs and applications that compile in a matter of seconds

Book Description

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.

What you will learn

  • Become familiar with the basic building blocks of programming in the V language
  • Install the V language on various operating systems
  • Understand how to work with arrays and maps in V programming
  • Discover how to implement concurrency in V programming
  • Use channels in V programming to learn the best practices of sharing memory by communicating among coroutines
  • Write modular code and build on your knowledge of structs and functions in V
  • Get acquainted with writing tests in V programming
  • Get to grips with building and querying RESTful microservice in V

Who this book is for

Whether you're a beginner interested in learning a programming language or an experienced programmer looking to switch to a new and better statically compiled programming language, this V programming book is for you.

Table of contents

  1. Getting Started with V Programming
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Section 1: Introduction to the V Programming Language
  7. Chapter 1: Introduction to V Programming
    1. The past, present, and future of V
    2. V is a statically typed and compiled programming language
    3. Simple and maintainable syntax
    4. Backward compatibility, stability, and easy to upgrade to future versions
    5. Features of V programming
      1. Performance
      2. Speed
      3. No null values
      4. No global variables
      5. No undefined values
      6. Error handling
      7. Powerful concurrency
      8. Easy cross-compilation
      9. V to JavaScript conversion
      10. Profiling
    6. V as a framework
      1. Memory management using the autofree engine
      2. Built-in ORM
      3. Built-in web server
      4. Native cross-platform GUI library
      5. Vinix – an OS kernel written in V
    7. Operating systems V supports
    8. Summary
  8. Chapter 2: Installing V Programming
    1. Technical requirements
    2. Installing V on the Windows OS
      1. Approach 1 – installing V from portable binaries on the Windows OS
      2. Approach 2 – installing V from source on the Windows OS
    3. Adding V to the environment variables in the Windows OS
    4. Accessing V programming using a REPL
    5. Installing V on the Linux OS (Ubuntu)
      1. Using Symlink V to make V accessible globally in Ubuntu
    6. Summary
  9. Section 2: Basics of V Programming
  10. Chapter 3: Variables, Constants, and Code Comments
    1. Technical requirements
    2. Understanding variables in V
      1. The variable naming convention
      2. Variable assignment
      3. Features of variables in V
      4. The limitations of variables in V
    3. Working with constants in V
      1. Naming conventions for constants
      2. Defining constants
      3. Defining complex constants
      4. Best practices when working with constants
    4. Variables versus constants
    5. Adding code comments in V
      1. Single-line comments
      2. Multiline comments
    6. Summary
  11. Chapter 4: Primitive Data Types
    1. Technical requirements
    2. Introducing primitive data types
    3. The Boolean data type
      1. Logical operators
      2. Relational operators
    4. Numeric data types
      1. Signed and unsigned integers
      2. Floating-point types
      3. Promoting numeric types
    5. Operations on numeric data types
      1. Arithmetic operators
      2. Bitwise operators
      3. Shift operators
    6. The string data type
      1. Working with the string data type
    7. The rune data type
    8. Operations on the string data type
      1. String interpolation
      2. String manipulation techniques
    9. Summary
  12. Chapter 5: Arrays and Maps
    1. Technical references
    2. Arrays
      1. Different methods to declare arrays
      2. Working with the len and cap array properties
      3. Accessing array elements using the index
      4. Accessing array elements using slices
      5. Operators used with arrays
      6. Fixed-size arrays
      7. Multidimensional arrays
      8. Performing various operations on an array
    3. Maps
      1. The explicit initialization of a map
      2. The short syntax initialization of a map
      3. The count of key-value pairs in a map
      4. Retrieving a value given the key of a map
      5. Accessing a non-existent key from a map
      6. Handling the retrieval of missing keys in a map
      7. Updating the value of the key in a map
      8. Deleting a key-value pair from a map
    4. Summary
  13. Chapter 6: Conditionals and Iterative Statements
    1. Technical requirements
    2. Conditional blocks
      1. The if statement
      2. match
    3. Iterative statements
      1. A for loop on maps
      2. A for loop on arrays
      3. A for loop without an index on the array
      4. A traditional C-style for loop
      5. A reverse for loop
      6. A for loop on a range
      7. A bare for loop or an infinite for loop
      8. Using break in a for loop
      9. Using continue in a for loop
      10. Using the continue and break statements with labels
    4. Summary
  14. Chapter 7: Functions
    1. Technical requirements
    2. Introducing functions
    3. Understanding function types
      1. The main function
      2. Basic functions
      3. Anonymous functions
      4. Higher-order functions
    4. Understanding function features
      1. Functions can return values or simply perform operations
      2. Functions can take zero or more input arguments
      3. Functions can return multiple values
      4. Ignoring return values from functions
      5. Functions can call other accessible functions
      6. Functions allow only arrays, interfaces, maps, pointers, and structs as mutable arguments
      7. Function declarations in script mode should come before all script statements
      8. Functions do not allow access to module variables or global variables
      9. Functions do not allow default or optional arguments
      10. Functions can have optional return types
      11. Functions are private by default and can be exposed to other modules using the pub access modifier keyword
      12. Functions allow you to defer the execution flow using the defer block
      13. Functions can be represented as elements of an array or a map
    5. Summary
  15. Chapter 8: Structs
    1. Technical requirements
    2. Introducing structs
      1. Defining a struct
      2. Initializing a struct
      3. Accessing the fields of a struct
      4. Understanding heap structs
    3. Updating the fields of a struct
    4. Approaches to defining struct fields
      1. Adding multiple mutable fields to a struct
      2. Grouping fields in a struct using access modifiers
      3. Defining required fields in a struct
      4. Defining struct fields with default values
    5. Defining methods for a struct
    6. Adding a struct as a field inside another struct
      1. Modifying the fields of struct type inside another struct
    7. Structs as trailing literal arguments to a function
    8. Summary
  16. Chapter 9: Modules
    1. Technical requirements
    2. Introducing modules
      1. The syntax to define a module
      2. The syntax to import a module
    3. Working with modules
      1. Creating a simple V project
      2. Creating a module
      3. Importing a module
      4. Accessing members of a module
      5. Working with multiple files in a module
      6. Member scope in the module
      7. Implications of cyclic imports
      8. The init function for a module
      9. Accessing the constants of a module
      10. Accessing structs and embedded structs of a module
    4. Summary
  17. Section 3: Advanced Concepts in V Programming
  18. Chapter 10: Concurrency
    1. Technical requirements
    2. Introducing concurrency
    3. Understanding parallelism
    4. Learning the basic terminology
    5. Getting started with concurrency
      1. Understanding the time module
      2. Understanding the thread type
    6. Implementing concurrency in V
      1. The go keyword syntax
    7. Spawning a void function to run concurrently
      1. Waiting on a concurrent thread
    8. Implementing a real-life concurrency scenario programmatically
      1. Running multiple tasks in a sequence
      2. Spawning multiple tasks to run concurrently
      3. Comparing sequential and concurrent program runtimes
    9. Learning different approaches to implement concurrent programs
      1. Spawning functions with return values to run concurrently
      2. Spawning anonymous functions to run concurrently
    10. Sharing data between the main thread and concurrent tasks
    11. Summary
  19. Chapter 11: Channels – An Advanced Concurrency Pattern
    1. Technical requirements
    2. Syntax to define a channel
      1. Unbuffered channel
      2. Buffered channel
    3. Channel operations
      1. Arrow operator <-
      2. Push operation
      3. Pop operation
    4. Channel properties
      1. Understanding channel properties using examples
    5. Channel methods
      1. Using try_push() on unbuffered channels
      2. Using try_push() on buffered channels
      3. try_pop()
      4. close()
    6. Working with unbuffered channels
      1. Understanding the blocking nature of unbuffered channels
      2. Dealing with the blocking behavior of unbuffered channels
      3. Synchronizing data between coroutines that communicate via an unbuffered channel
    7. Working with buffered channels
      1. Understanding the behavior of a buffered channel
      2. Establishing buffered communication channel between coroutines
      3. Synchronizing data between coroutines that communicate through a buffered channel
    8. Channel select
    9. Summary
  20. Chapter 12: Testing
    1. Technical requirements
    2. Introduction to tests in V
      1. The assert keyword
      2. Writing a simple test
      3. Running tests
    3. Understanding testsuite functions
      1. Demonstrating the usage of testsuite functions
    4. The AAA pattern of writing tests
    5. Writing tests for functions with optional return types
    6. Approaches to writing and running tests
      1. Writing tests for a simple program
      2. Running tests contained in _test.v file
      3. Writing tests for a project with modules
      4. Running tests contained in a module
      5. Writing tests for members of a sub-module from the main module
      6. Running all the tests contained in a project
      7. Running tests with stats
    7. Summary
  21. Chapter 13: Introduction to JSON and ORM
    1. Technical requirements
    2. Getting started with JSON
      1. Decoding a JSON object
      2. Encoding an object into JSON data
    3. Learning ORM
      1. Understanding ORM attributes
      2. Creating a struct for ORM
      3. Working with the ORM library
      4. Briefly understanding database operations using V's ORM
      5. Performing DDL operations using V's ORM
      6. Performing DML operations using V's ORM
    4. Summary
  22. Chapter 14: Building a Microservice
    1. Technical requirements
    2. Introducing vweb
    3. Creating a project and organizing files
    4. Setting up the vweb web server
    5. Setting up utility functions and constants for the microservice
    6. Implementing RESTful endpoints
      1. The list of RESTful endpoints in our microservice
      2. Defining the Note struct
    7. Implementing an endpoint to create a note using HTTP verb POST
      1. Defining the route to create a note
      2. Processing requests and handling custom responses for the create endpoint
      3. Inserting a record using the ORM query
      4. Building a response body for the create endpoint
    8. Implementing an endpoint to retrieve a note by id using HTTP verb GET
      1. Defining a route to retrieve a note by id
      2. Selecting a record given its id using the ORM query
      3. Handling a custom response for the read a note endpoint
      4. Building a response body for the read endpoint
    9. Implementing an endpoint to retrieve all notes using HTTP verb GET
      1. Defining a route to retrieve all notes
      2. Selecting all the records from a table using the ORM query
      3. Building a response body for the read all notes endpoint
    10. Implementing an endpoint to update a note using HTTP verb PUT
      1. Defining a route to update a note by id
      2. Processing requests and handling custom responses for the update endpoint
      3. Verifying a record exits given its id using the ORM query
      4. Verifying the uniqueness of the message field of note
      5. Updating the record using the ORM query
      6. Building a response for the update endpoint
    11. Implementing an endpoint to delete a note using the HTTP verb DELETE
      1. Defining the route to delete a note by id
      2. Deleting a record given its id using the ORM query
      3. Building a response for the delete endpoint
      4. Running the microservice
    12. Querying REST endpoints using Postman
      1. Using Postman to create a note with the POST HTTP verb
      2. Using Postman to retrieve a note by id with the GET HTTP verb
      3. Using Postman to retrieve a collection of notes with the GET HTTP verb
      4. Using Postman to update a note with the PUT HTTP verb
      5. Using Postman to delete a note with the DELETE HTTP verb
    13. Summary
    14. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Getting Started with V Programming
  • Author(s): Navule Pavan Kumar Rao
  • Release date: December 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781839213434