One place for hosting & domains

      Program

      How To Write Your First Program in Go


      Introduction

      The “Hello, World!” program is a classic and time-honored tradition in computer programming. It’s a simple and complete first program for beginners, and it’s a good way to make sure your environment is properly configured.

      This tutorial will walk you through creating this program in Go. However, to make the program more interesting, you’ll modify the traditional “Hello, World!” program so that it asks the user for their name. You’ll then use the name in the greeting. When you’re done with the tutorial, you’ll have a program that looks like this when you run it:

      Output

      Please enter your name. Sammy Hello, Sammy! I'm Go!

      Prerequisites

      Before you begin this tutorial, you will need a local Go development environment set up on your computer. You can set this up by following one of these tutorials:

      Step 1 — Writing the Basic “Hello, World!” Program

      To write the “Hello, World!” program, open up a command-line text editor such as nano and create a new file:

      Once the text file opens up in the terminal window, you’ll type out your program:

      hello.go

      package main
      
      import "fmt"
      
      func main() {
        fmt.Println("Hello, World!")
      }
      

      Let’s break down the different components of the code.

      package is a Go keyword that defines which code bundle this file belongs to. There can be only one package per folder, and each .go file has to declare the same package name at the top of its file. In this example, the code belongs to the main package.

      import is a Go keyword that tells the Go compiler which other packages you want to use in this file. Here you import the fmt package that comes with the standard library. The fmt package provides formatting and printing functions that can be useful when developing.

      fmt.Println is a Go function, found in the fmt package, that tells the computer to print some text to the screen.

      You follow the fmt.Println function by a sequence of characters, like "Hello, World!", enclosed in quotation marks. Any characters that are inside of quotation marks are called a string. The fmt.Println function will print this string to the screen when the program runs.

      Save and exit nano by typing CTRL + X, when prompted to save the file, press Y.

      Now you can try your program.

      Step 2 — Running a Go Program

      With your “Hello, World!” program written, you're ready to run the program. You’ll use the go command, followed by the name of the file you just created.

      The program will execute and display this output:

      Output

      Hello, World!

      Let's explore what actually happened.

      Go programs need to compile before they run. When you call go run with the name of a file, in this case hello.go, the go command will compile the application and then run the resulting binary. For programs written in compiled programming languages, a compiler will take the code from a program and generate another type of lower-level code (such as source code or machine code) to produce an executable program.

      Go applications require a main package and exactly one main() function that serves as the entry point for the application. The main function takes no arguments and returns no values. Instead it tells the Go compiler that the package should be compiled as an executable package.

      Once compiled, the code executes by entering the main() function in the main package. It executes the line fmt.Println("Hello, World!") by calling the fmt.Println function. The string value of Hello, World! is then passed to the function. In this example, the string Hello, World! is also called an argument since it is a value that is passed to a method.

      The quotes that are on either side of Hello, World! are not printed to the screen because you use them to tell Go where your string begins and ends.

      In this step, you've created a working "Hello, World!" program with Go. In the next step, you will explore how to make the program more interactive.

      Step 3 — Prompting for User Input

      Every time you run your program, it produces the same output. In this step, you can add to your program to prompt the user for their name. You'll then use their name in the output.

      Instead of modifying your existing program, create a new program called greeting.go with the nano editor:

      First, add this code, which prompts the user to enter their name:

      greeting.go

      package main
      
      import (
          "fmt"
      )
      
      func main() {
        fmt.Println("Please enter your name.")
      }
      

      Once again, you use the fmt.Println function to print some text to the screen.

      Now add the highlighted line to store the user's input:

      greeting.go

      package main
      
      import (
          "fmt"
      )
      
      func main() {
        fmt.Println("Please enter your name.")
        var name string
      }
      

      The var name string line will create a new variable using the var keyword. You name the variable name, and it will be of type string.

      Then, add the highlighted line to capture the user's input:

      greeting.go

      package main
      
      import (
          "fmt"
      )
      
      func main() {
        fmt.Println("Please enter your name.")
        var name string
        fmt.Scanln(&name)
      }
      

      The fmt.Scanln method tells the computer to wait for input from the keyboard ending with a new line or (n), character. This pauses the program, allowing the user to enter any text they want. The program will continue when the user presses the ENTER key on their keyboard. All of the keystrokes, including the ENTER keystroke, are then captured and converted to a string of characters.

      You want to use those characters in your program's output, so you save those characters by writing them into the string variable called name. Go stores that string in your computer's memory until the program finishes running.

      Finally, add the following highlighted line in your program to print the output:

      greeting.go

      package main
      
      import (
          "fmt"
      )
      
      func main() {
        fmt.Println("Please enter your name.")
        var name string
        fmt.Scanln(&name)
        fmt.Printf("Hi, %s! I'm Go!", name)
      }
      

      This time, instead of using the fmt.Println method again, you're using fmt.Printf. The fmt.Printf function takes a string, and using special printing verbs, (%s), it injects the value of name into the string. You do this because Go does not support string interpolation, which would let you take the value assigned to a variable and place it inside of a string.

      Save and exit nano by pressing CTRL + X, and press Y when prompted to save the file.

      Now run the program. You'll be prompted for your name, so enter it and press ENTER. The output might not be exactly what you expect:

      Output

      Please enter your name. Sammy Hi, Sammy ! I'm Go!

      Instead of Hi, Sammy! I'm Go!, there's a line break right after the name.

      The program captured all of our keystrokes, including the ENTER key that we pressed to tell the program to continue. In a string, pressing the ENTER key creates a special character that creates a new line. The program's output is doing exactly what you told it to do; it's displaying the text you entered, including that new line. It's not what you expected the output to be, but you can fix it with additional functions.

      Open the greeting.go file in your editor:

      Locate this line in your program:

      greeting.go

      ...
      fmt.Scanln(&name)
      ...
      

      Add the following line right after it:

      greeting.go

      name = strings.TrimSpace(name)
      

      This uses the TrimSpace function, from Go's standard library strings package, on the string that you captured with fmt.Scanln. The strings.TrimSpace function removes any space characters, including new lines, from the start and end of a string. In this case, it removes the newline character at the end of the string created when you pressed ENTER.

      To use the strings package you need to import it at the top of the program.

      Locate these lines in your program:

      greeting.go

      import (
          "fmt"
      )
      

      Add the following line to import the strings package:

      greeting.go

      import (
          "fmt"
        "strings"
      )
      

      Your program will now contain the following:

      greeting.go

      package main
      
      import (
              "fmt"
              "strings"
      )
      
      func main() {
          fmt.Println("Please enter your name.")
          var name string
          fmt.Scanln(&name)
          fmt.Printf("Hi, %s! I'm Go!", name)
          name = strings.TrimSpace(name)
      }
      

      Save and exit nano. Press CTRL + X, then press Y when prompted to save the file.

      Run the program again:

      This time, after you enter your name and press ENTER, you get the expected output:

      Output

      Please enter your name. Sammy Hi, Sammy! I'm Go!

      You now have a Go program that takes input from a user and prints it back to the screen.

      Conclusion

      In this tutorial, you wrote a "Hello, World!" program that takes input from a user, processes the results, and displays the output. Now that you have a basic program to work with, try to expand your program further. For example, ask for the user's favorite color, and have the program say that its favorite color is red. You might even try to use this same technique to create a simple Mad-Lib program.



      Source link

      How to Create a Loyalty Program for Your Website (And Why You Should)


      If you’re anything like us, you value your website visitors and customers. After all, they’re the reason for your success. If they don’t feel inclined to keep visiting your site or doing business with you, your conversions and sales will likely take a steep dive.

      This is where starting up a loyalty program can be immensely useful. By rewarding your customers directly for their continued interest in your brand, you can make them feel more appreciated while increasing your own profits at the same time.

      In this article, we’ll introduce you to loyalty programs and explain why they’re such an effective tool. We’ll also discuss how you can begin your own, and talk about what you need to know before doing so. Punch cards not required.

      What a Loyalty Program Is (And Why You Should Consider Creating One)

      A rewards program on the Starbucks website.

      Chances are you don’t need much of an introduction to loyalty programs. You may even have a loyalty card or two in your wallet right now. As it turns out, the concept isn’t much different whether you apply it to a physical coffee shop or a website. Ultimately, the goal is to make customers feel like they are members of an exclusive club and reward them in a way that encourages them to keep coming back.

      The idea of using this type of loyalty marketing is actually centuries old. Back in the late 1700s, merchants used to give out copper tokens to customers, which they could exchange for other products later on. In some ways, this is not far removed from how a lot of loyalty programs work today.

      By giving a customer reasons to come back to your store, as well as incentivizing repeat purchases, you create a positive feedback loop. The more they spend, the more they get rewarded, encouraging them to make even more purchases. This works out in everyone’s favor, as you get more business and your users are recognized for their continued patronage.

      There are a number of ways to apply this formula to websites. The classic token method is one option, or you could implement a points program to ‘gamify’ your site. We’ll look at these and other techniques later in this article.

      Before that, let’s summarize the main benefits of running a loyalty program. In short, loyalty marketing enables you to:

      • Retain your customers more productively. By offering rewards, you can give users a tangible reason to keep coming back to your site and making repeat purchases. If you’re running a small business, customer retention is critical.
      • Provide genuine value to your customer base. A loyalty program can help make customers feel appreciated, as they are rewarded with gifts, premiums, additional content, and so on.
      • Increase the likelihood that customers will refer your site to their networks. If you build a strong relationship with your current users, they’ll be more inclined to recommend your site to others, increasing your profits in the process. Word-of-mouth marketing is an important tool to have in your arsenal.
      • Make your business more desirable to consumers. By making the benefits of long-term loyalty to your small business clear, it can appear more welcoming and appealing to newcomers.
      • Save money on marketing to existing customers. Loyalty programs are often cost-effective measures, which can be very easy to maintain over time.

      While we’ve talked a lot about “customers” so far, it’s essential to note that customer loyalty programs are not just for stores or other commercial websites. You can run a successful program for any business or website where you want to encourage your users to act in some way.

      What’s more, there are many different types of loyalty programs you can model yours after. Let’s take a closer look at some of the best options.

      The Different Types of Customer Loyalty Programs

      Loyalty programs can come in many shapes and sizes. As such, the first thing you need to do before starting one is to decide what type of program you’d like to run, and what would suit your website best. In fact, you can even combine aspects of different kinds of programs, to create something completely unique.

      Determining what type of rewards system you want your program to focus on is the most critical consideration, as it will change everything about how you design it. To provide you with some inspiration, let’s take a look at a few of the most popular types of rewards systems.

      Points Program

      This is the type of customer loyalty program you’re most likely already familiar with. These programs give users loyalty points when they perform specific actions, most commonly completing a purchase. Those points can then be exchanged for gifts, discounts, or other non-monetary rewards.

      One notable example of this in action is Nintendo’s rewards program.

      The My Nintendo home page.

      Nintendo uses a system where users can earn points both by buying products and by simply playing the games they’ve bought and connected to their accounts. The company also offers two different types of points, which can be exchanged for various physical rewards.

      Not only does this reward users for spending money on Nintendo products, it actively encourages them to play the games they’ve purchased. This is an excellent example of how a loyalty program can be beneficial to both business owners and the customer, and can also promote long-term engagement.

      Gamification

      Turning your website into a game might seem drastic, but it’s become a very popular way to engage with visitors. In a sense, gamification is the newer cousin of the points program. Users are not just rewarded for certain actions, but they are actually able to progress in some way. For example, such a system could contain ‘levels’ or ‘tiers,’ each of which grants permanent privileges when a user reaches it.

      Arguably one of the most famous examples of this in action is the McDonald’s Monopoly game.

      The McDonald’s Monopoly game website.

      This is an annual event where McDonald’s customers can collect game pieces, which they can exchange for free food items, or save up to potentially win bigger prizes. Pieces also vary in rarity, giving customers a reason to keep coming back and spending more. That way, they’ll increase their chances of completing a set or finding a rare piece.

      If fast food isn’t your thing, you may instead be familiar with the system used by Stack Overflow.

      The Stack Overflow badges.

      On the Stack website, you can earn reputation and points by performing specific actions that are helpful to the community. As your reputation grows, your level increases, which grants you access to new privileges. In other words, this program incentivizes and encourages helping out the community. Users who put in the effort are rewarded with more insight and tools.

      Rewards Partnership

      It’s not just businesses and users who can have mutually beneficial relationships, as this next type of program demonstrates. A rewards partnership is a program where two (or more) brands collaborate to provide rewards to their respective users.

      We’ve already seen one example of this with the Monopoly game, which is a collaboration between McDonald’s and Hasbro. However, there are many ways you can use this type of program to your advantage.

      For example, two companies could each reward their loyal customers with products or services provided by the partner business. This can be particularly useful for businesses that have limited interaction opportunities with customers, such as insurance companies or banks.

      An excellent example of this is the partnership between Spotify and Starbucks.

      The Spotify and Starbucks rewards program.

      As part of this program, Starbucks Rewards members can get Spotify Premium with a significant discount. If they do sign up for a Spotify account, they’ll also get rewards points they can use at Starbucks, creating a ‘virtuous circle’ and great customer experience.

      Charity Program

      Finally, you can use a loyalty program not just to benefit you and your users. You can also leverage it to help underprivileged people. You can do this by implementing a charity-based program, where the rewards take the form of donations and other charity efforts.

      For a glimpse into how this can work, take a look at the Improving Lives program from TOMS.

      The TOMS Improving Lives program.This footwear brand helps many charitable causes as a direct result of purchases made by customers. Every time somebody buys one of its products, a share of the profits will go to providing clean water, shoes, and other types of support to various regions all over the world.

      This type of program is an excellent use of a brand as a positive force in the world. What’s more, it builds trust in your business and may make people more likely to want to work with you.

      How to Create a Loyalty Program for Your Website (In 4 Steps)

      As you can probably imagine, setting up and implementing a loyalty program can be a complicated process, no matter which variety you opt for. To help you get started, we’re going to walk you through the basic steps of how to create a loyalty program and share some tools that can speed up the process.

      Step 1: Choose a Name and Set Up a Domain

      When starting any loyalty program or rewards club, it’s a smart idea to come up with a name that communicates its purpose immediately. In the same way that choosing a strong domain name benefits your website, giving your program the right moniker is vital.

      The most important consideration is to be descriptive. Clearly tie the name in with your brand, but also make the purpose of your program obvious. Sometimes this can be as simple as a name like “Starbucks Rewards,” but you can also get creative.

      For instance, framing your program as a ‘club’ provides an added layer of trust and commitment. This is a way to take a simple rewards system and turn it into a community, where dedicated members are rewarded for their ongoing loyalty.

      To really make an impact on users, you can even get a .CLUB domain for your rewards program.

      Buying a .CLUB domain with DreamHost.

      This can help to connect your program with your brand, while also making its purpose clear. Whether you have a dedicated website for your loyalty program or a section of your current website devoted to it, having a custom domain for your loyalty brand makes it easier for your customers to learn how to participate. For example, the watch company Swatch has a “Swatch Club” loyalty program, and the domain www.swatch.club points to the section of their website that is all about the club. If you do require a separate domain and hosting for your loyalty program, you’ll also want to be sure and select a high-quality hosting plan.

      Step 2: Specify Your Program’s Goals

      We’ve already outlined some of the ways you can implement a loyalty program. The method you decide to use will depend entirely on your ultimate goals. To put it simply: What do you hope to get out of the program? You’ll need to consider this question carefully if you want to find the solution that’ll work best for you.

      For instance, if you want your program to merely increase the number of repeat purchases, a simple points system is probably enough. This will encourage users to come back multiple times after their first order. However, as we covered earlier, that’s just one way you can use a rewards program.

      For instance, the Stack Overflow program we mentioned before is essentially a way to crowdsource community management. It provides next-to-no financial incentives to users, but it helps them get involved in the community while letting the site keep its support costs down.

      In short, you need to know what your program’s ultimate purpose is, so you can find the solution that will work best to achieve that goal. Feel free to look at the examples we outlined earlier, and use them as inspiration for your own plans.

      Step 3: Implement Your Rewards (And Make Them Visible)

      Once you know what your program’s focus is, you can start to think about rewards. You can consider this stage as effectively being the practical application of the previous step.

      You’ll need to consider who you’re targeting with the program, and what you want to encourage them to do. Then, you’ll have to present them with a variety of rewards that align with your users’ values. Depending on your brand and intentions, this could be exclusive content, special deals, early access to new products, and much more.

      A key part of this step is making sure the rewards you offer are well defined and visible on your site. For instance, the Boots store clearly displays the monetary value of the points you can earn by purchasing individual products.

      The Boots website, showing several products and their points values. This will not only inform your current members but will also serve as an advertisement for your program. By making the benefits of your rewards program clear, you’ll encourage new users to sign up.

      Step 4: Make It Easy to Enroll in Your Program

      Last but not least, signing up to your program needs to be as easy as possible. After all, you’ll want to place few barriers between your users and your program. Fortunately, if you’re using WordPress, this becomes significantly easier.

      That’s because WordPress can actually take care of the registrations for you, if you have the right tools. For instance, you can use a plugin to apply the features of a loyalty program right onto your existing site. One example of this kind of plugin is myCRED.

      The myCRED home page.

      This tool adds a scoring system to your site, and each registered member is automatically assigned a point balance. You can then specify which interactions should reward users with points, and how many they’ll get.

      Another plugin that does something similar is Gratisfaction.

      The Gratisfaction plugin.

      This plugin adds a fully-featured loyalty program to your site, enabling you to set up just about any scoring or rewards system you’d like. Once again, it uses WordPress’ built-in user system, and simply layers the loyalty features on top of the platform’s existing functionality. This is a much quicker and simpler way to implement a rewards program than designing one from scratch, while still remaining flexible.

      Conclusion

      Maintaining a positive and lasting relationship with your customers couldn’t be more critical. The best way to do this is to make them feel valued and appreciated — as if being your customer makes them members of a special club — which you can accomplish by implementing a loyalty program. This lets you reward your loyal customers for taking desired actions and can also lead to increased profits for you in the long term.

      Do you have any questions about loyalty programs, or how to build an effective one? Join our DreamHost Community or hit us up on social media to chat about it!



      Source link