i Golang introduce – All things in moderation

Golang introduce

Go language is a programming language based on systems programming, initially developed at Google in the year 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is a statically-typed language having the syntax similar to that of C, C++. It provides garbage collection, type safety, dynamic-typing capability, many advanced built-in types such as variable length arrays and key-value maps. It also provides a rich standard library. The Go programming language was launched in November 2009 and is used in some of Google’s production systems.

Some features

Declaration variable with implicit type

Syntax similar to that of C.

Simple, concise, and safe.

Compilation time is fast.

Compiled languages tend to run faster and the executable can be run without additional dependencies (at least, that’s true for languages like C, C++ and Go which compile directly to assembly.

Not supported

Doesn’t support for type inheritance

Doesn’t support for method or operator overloading

Doesn’t support for circular dependencies among packages

Doesn’t support for pointer arithmetic

Doesn’t support for assertions

Doesn’t support for generic programming

**Install: **

Download and install Go Stable versions: https://golang.org/dl/

Set up two environment variables:

OSX / Linux:

GOPATH points to your workspace, for me, that’s $HOME/code/go. We need to append Go’s binary to our PATH
You can set these up from a shell:

echo 'export GOPATH=$HOME/code/go' >> $HOME/.profile
echo 'export PATH=$PATH:/usr/local/go/bin' >> $HOME/.profile

Windows:

GOPATH points to your workspace. That might be something like c:\users\windows\go.

Add c:\Go\bin to your PATH environment variable.

Environment variables can be set through control panel => system => Advanced system settings

$  go help
Go is a tool for managing Go source code.

Usage:

        go <command> [arguments]

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         download and install packages and dependencies
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

        buildmode   build modes
        c           calling between Go and C
        cache       build and test caching
        environment environment variables
        filetype    file types
        go.mod      the go.mod file
        gopath      GOPATH environment variable
        gopath-get  legacy GOPATH go get
        goproxy     module proxy protocol
        importpath  import path syntax
        modules     modules, module versions, and more
        module-get  module-aware go get
        packages    package lists and patterns
        testflag    testing flags
        testfunc    testing functions

Use "go help <topic>" for more information about that topic.

Running Hello world programs

Our first program will print the “hello world” message.

hello-world.go

package main

import (
    "fmt"
)
func main() {
    fmt.Println("hello world")
}

To run the program use go run command.

$ go run hello-world.go
hello world

Sometimes we’ll want to build our programs into binaries. We can do this using go build.

$ go build hello-world.go
$ ls
hello-world hello-world.go

$ ./hello-world
hello world

Leave a Reply