Helion


Szczegóły ebooka

Getting Started with V Programming

Getting Started with V Programming


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.

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