Go Lang Basics Series #1

4 min read
go-lang-basics-series-1

the most promising programming language for modern application

The Basics concept introduced Go as a statically typed, compiled programming language. The language is often referred to as Golang because of its domain name, golang.org, but the proper name is Go.

The Basics concept introduces three major language features: Packages, Functions, and Variables.

Packages

Go applications are organized in packages. A package is a collection of source files located in the same directory. All source files in a directory must share the same package name. It is conventional for the package name to be the last directory in the import path. For example, the files in the "math/rand" package begin with the statement package rand. When a package is imported, only entities (functions, types, variables, constants) whose names start with a capital letter can be used / accessed. The recommended style of naming in Go is that identifiers will be named using camelCase, except for those meant to be accessible across packages which should be CamelCase.

example:

package loline -- this means get me everthing possible from loline package

Variables

Go is statically-typed, which means all variables must have a defined type at compile-time.

Variables can be defined by explicitly specifying a type:

var explicit int // this is telling the compiler explicitly the variable is a type of integer

You can also use an initializer, and the compiler will assign the variable type to match the type of the initializer.

implicit := 10  // Implicitly typed as an int

attention

Once declared, variables can be assigned values using the = operator. Once declared, a variable's type can never change.

count := 1 // Assign initial value count = 2 // Update to new value count = false // This throws a compiler error due to assigning a non `int` type

Constants

Constants hold a piece of data just like variables, but their value cannot change during the execution of the program.

Constants are defined using the const keyword and can be numbers, characters, strings or booleans:

example:

const pi=3.14

Functions

Go functions accept zero or more parameters. Parameters must be explicitly typed, there is no type inference.

Values are returned from functions using the return keyword.

A function is invoked by specifying the function name and passing arguments for each of the function's parameters.

Note that Go supports two types of comments. Single line comments are preceded by // and multiline comments are inserted between /* and */.

package greeting // Hello is a public function func Hello (name string) string { return hi(name) } // hi is a private function func hi (name string) string { return "hi " + name }

in the next article we will solve some challenges from https://exercism.org/

FYI: the contents are from https://exercism.org/ you can visit the go lang track

second article link

https://blog.lolinemag.com/article/ea2b5b0b-cba0-44d9-b782-679fdabc96b2/

Comments (0)
No comments yet