Text Templates in Visual Basic .NET

Visual Basic .NET offers built-in support for creating dynamic content or showing customized output to the user with the System.Text.StringBuilder class. This class provides a mutable string of characters, which can be used to efficiently construct strings.

Imports System
Imports System.Text

Module TextTemplates
    Sub Main()
        ' We can create a new StringBuilder and append text to it.
        ' StringBuilder is a mutable string of characters.
        Dim t1 As New StringBuilder()
        t1.AppendFormat("Value is {0}" & vbNewLine, "some text")

        ' We can use String.Format for simple string formatting
        Console.WriteLine(String.Format("Value: {0}", "some text"))
        Console.WriteLine(String.Format("Value: {0}", 5))
        Console.WriteLine(String.Format("Value: {0}", String.Join(", ", {"VB.NET", "C#", "F#", "C++"})))

        ' If the data is a class, we can use string interpolation to access its properties.
        ' The properties should be public to be accessible.
        Dim person = New With {.Name = "Jane Doe"}
        Console.WriteLine($"Name: {person.Name}")

        ' The same applies to dictionaries
        Dim dict As New Dictionary(Of String, String) From {
            {"Name", "Mickey Mouse"}
        }
        Console.WriteLine($"Name: {dict("Name")}")

        ' Conditional execution can be done using If statements
        Dim t3 As New StringBuilder()
        If Not String.IsNullOrEmpty("not empty") Then
            t3.Append("yes")
        Else
            t3.Append("no")
        End If
        t3.AppendLine()
        Console.Write(t3.ToString())

        t3.Clear()
        If Not String.IsNullOrEmpty("") Then
            t3.Append("yes")
        Else
            t3.Append("no")
        End If
        t3.AppendLine()
        Console.Write(t3.ToString())

        ' For loops let us iterate through collections
        Dim languages() As String = {"VB.NET", "C#", "F#", "C++"}
        Console.Write("Range: ")
        For Each lang In languages
            Console.Write(lang & " ")
        Next
        Console.WriteLine()
    End Sub
End Module

To run the program, save the code in a file with a .vb extension (e.g., TextTemplates.vb) and use the VB.NET compiler to compile it:

$ vbc TextTemplates.vb
$ mono TextTemplates.exe
Value: some text
Value: 5
Value: VB.NET, C#, F#, C++
Name: Jane Doe
Name: Mickey Mouse
yes
no
Range: VB.NET C# F# C++

In this example, we’ve used StringBuilder and string formatting to achieve similar functionality to Go’s text templates. VB.NET doesn’t have a direct equivalent to Go’s template package, but we can use string manipulation and formatting to achieve similar results.

The StringBuilder class is used for efficient string concatenation, while String.Format and string interpolation (using $) are used for simple string formatting.

Conditional execution is done using standard If/Else statements, and iteration is performed using For Each loops.

Note that VB.NET doesn’t have an exact equivalent to Go’s template package, so we’ve used similar concepts to achieve the same results. The approach here is more imperative than Go’s declarative template approach, but it allows for similar functionality.