Riley Lambert

Riley Lambert

1555038565

How To Write Your First Program in Go?

#go

What is GEEK

Buddha Community

Edward Jackson

1555038863

This tutorial will walk you through creating this 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:

OutputPlease 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:

nano hello.go


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.

go run hello.go


The program will execute and display this output:

OutputHello, 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:

nano greeting.go


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:

OutputPlease 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:

nano greeting.go


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:

go run greeting.go


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

OutputPlease 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.

Thanks for reading ❤

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Learn More

Learn How To Code: Google’s Go (golang) Programming Language

Go: The Complete Developer’s Guide (Golang)

Build Realtime Apps | React Js, Golang & RethinkDB

Why Go Is Perfect For DevOps

Google’s Go Essentials For Node.js / JavaScript Developers

Moving from NodeJS to Go

Build a chat app with Go

*Originally published by Gopher Guides at *https://www.digitalocean.com

Fannie  Zemlak

Fannie Zemlak

1599854400

What's new in the go 1.15

Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.

As Go promise for maintaining backward compatibility. After upgrading to the latest Go 1.15 version, almost all existing Golang applications or programs continue to compile and run as older Golang version.

#go #golang #go 1.15 #go features #go improvement #go package #go new features

Zander  Herzog

Zander Herzog

1596793260

Secure HTTPS servers in Go

In this article, we are going to look at some of the basic APIs of the http package to create and initialize HTTPS servers in Go.

Image for post

(source: unsplash.com)

In the “Simple Hello World Server” lesson, we learned about net/http package, how to create routes and how [ServeMux](https://golang.org/pkg/net/http/#ServeMux) works. In the “Running multiple HTTP servers” lesson, we learned about [Server](https://golang.org/pkg/net/http/#Server) structure and how to run multiple HTTP servers concurrently.

In this lesson, we are going to create an HTTPS server using both Go’s standard server configuration and custom configuration (using [_Server_](https://golang.org/pkg/net/http/#Server) structure). But before this, we need to know what HTTPS really is?

HTTPS is a big topic of discussion in itself. Hence while writing this lesson, I published an article just on “How HTTPS works?”. I advise you to read this lesson first before continuing this article. In this article, I’ve also described the encryption paradigm and SSL certificates generation process.


If we recall the simplest HTTP server example from previous lessons, we only need http.``[ListenAndServe](https://golang.org/pkg/net/http/#ListenAndServe) function to start an HTTP server and http.``[HandleFunc](https://golang.org/pkg/net/http/#HandleFunc) to register a response handler for a particular endpoint.

Image for post

(https://play.golang.org/p/t3sOenOYAzS)

In the example above, when we run the command go run server.go , it will start an HTTP server on port 9000. By visiting http://localhost:9000 URL in a browser, you will be able to see a Hello World! message on the screen.

Image for post

(http://localhost:9000)

As we know, the nil argument to ListenAndServe() call invokes Go to use the [DefaultServeMux](https://golang.org/pkg/net/http/#DefaultServeMux) response multiplexer, which is the default instance of ServeMux structure provided globally by the Go. The HandleFunc() call adds a response handler for a specific route on the multiplexer instance.

The http.ListenAndServe() call uses the Go’s standard HTTP server configuration, however, in the previous lesson, how we can customize a server using [Server](https://golang.org/pkg/net/http/#Server) structure type.

To start an HTTPS server, all we need do is to call ServerAndListenTLS method with some configuration. Just like ServeAndListen method, this method is available on both the http package and the Server structure.

The http.``[ServeAndListenTLS](https://golang.org/pkg/net/http/#ListenAndServeTLS) method uses the Go’s standard server implementation, however, both [Server](https://golang.org/pkg/net/http/#Server) instance and Server.``[ServeAndListenTLS](https://golang.org/pkg/net/http/#Server.ListenAndServeTLS) method can be configured for our needs.

#go-programming-language #go #golang-tutorial #go-programming #golang

Learning Go — Array, Slice, Map

We have already seen how to create and work with variables, pointers, and Constants. In this article, we are going to see the four important collection types built into the language.
We start with humble Arrays, from there we move on to Slice, an evolved version of Array. From Slice, we talk about the map a key-value pair collection. Let’s start with Arrays here.

#golang-tutorial #golang #go-programming #go-programming-language #programming

What You Can Learn about Setting from Classic Sitcoms

Giving your novel a strong sense of place is vital to doing your part to engage the readers without confusing or frustrating them. Setting is a big part of this (though not the whole enchilada — there is also social context and historic period), and I often find writing students and consulting clients erring on one of two extremes.

**Either: **Every scene is set in a different, elaborately-described place from the last. This leads to confusion (and possibly exhaustion and impatience) for the reader, because they have no sense of what they need to actually pay attention to for later and what’s just…there. Are the details of that forest in chapter 2 important? Will I ever be back in this castle again? Is there a reason for this character to be in this particular room versus the one she was in the last time I saw her? Who knows!

Or: There are few or no clues at all as to where the characters are in a scene. What’s in the room? Are they even in a room? Are there other people in th — ope, yes, there are, someone just materialized, what is happening? This all leads to the dreaded “brains in jars” syndrome. That is, characters are only their thoughts and words, with no grounding in the space-time continuum. No one seems to be in a place, in a body, at a time of day.

Everything aspect of writing a novel comes with its difficulties, and there are a lot of moving pieces to manage and deploy in the right balance. When you’re a newer writer, especially, there’s something to be said for keeping things simple until you have a handle on how to manage the arc and scope of a novel-length work. And whether you tend to overdo settings or underdo them, you can learn something from TV, especially classic sitcoms.

Your basic “live studio audience” sitcoms are performed and filmed on sets built inside studios vs. on location. This helps keep production expenses in check and helps the viewer feel at home — there’s a reliable and familiar container to hold the story of any given episode. The writers on the show don’t have to reinvent the wheel with every script.

Often, a show will have no more than two or three basic sets that are used episode to episode, and then a few other easily-understood sets (characters’ workplaces, restaurants, streets scenes) are also used regularly but not every episode.

#creative-writing #writing-exercise #writing-craft #writing #writing-tips #machine learning

Shawn  Durgan

Shawn Durgan

1595547778

10 Writing steps to create a good project brief - Mobile app development

Developing a mobile application can often be more challenging than it seems at first glance. Whether you’re a developer, UI designer, project lead or CEO of a mobile-based startup, writing good project briefs prior to development is pivotal. According to Tech Jury, 87% of smartphone users spend time exclusively on mobile apps, with 18-24-year-olds spending 66% of total digital time on mobile apps. Of that, 89% of the time is spent on just 18 apps depending on individual users’ preferences, making proper app planning crucial for success.

Today’s audiences know what they want and don’t want in their mobile apps, encouraging teams to carefully write their project plans before they approach development. But how do you properly write a mobile app development brief without sacrificing your vision and staying within the initial budget? Why should you do so in the first place? Let’s discuss that and more in greater detail.

Why a Good Mobile App Project Brief Matters?

Why-a-Good-Mobile-App-Project-Brief-Matters

It’s worth discussing the significance of mobile app project briefs before we tackle the writing process itself. In practice, a project brief is used as a reference tool for developers to remain focused on the client’s deliverables. Approaching the development process without written and approved documentation can lead to drastic, last-minute changes, misunderstanding, as well as a loss of resources and brand reputation.

For example, developing a mobile app that filters restaurants based on food type, such as Happy Cow, means that developers should stay focused on it. Knowing that such and such features, UI elements, and API are necessary will help team members collaborate better in order to meet certain expectations. Whether you develop an app under your brand’s banner or outsource coding and design services to would-be clients, briefs can provide you with several benefits:

  • Clarity on what your mobile app project “is” and “isn’t” early in development
  • Point of reference for developers, project leads, and clients throughout the cycle
  • Smart allocation of available time and resources based on objective development criteria
  • Streamlined project data storage for further app updates and iterations

Writing Steps to Create a Good Mobile App Project Brief

Writing-Steps-to-Create-a-Good-Mobile-App-Project-Brief

1. Establish the “You” Behind the App

Depending on how “open” your project is to the public, you will want to write a detailed section about who the developers are. Elements such as company name, address, project lead, project title, as well as contact information, should be included in this introductory segment. Regardless of whether you build an in-house app or outsource developers to a client, this section is used for easy document storage and access.

#android app #ios app #minimum viable product (mvp) #mobile app development #web development #how do you write a project design #how to write a brief #how to write a project summary #how to write project summary #program brief example #project brief #project brief example #project brief template #project proposal brief #simple project brief template