GoLang tidbits

go

GoLang tidbits

  1. How to remove go installation on Mac?
  2. Go Language Server
  3. Configuring Visual Studio Code to use Go Language Server
  4. Changing user name and email in VSCode
  5. Range looping in Go
  6. Shorthand Variable Declaration Limitation

How to remove go installation on Mac?

Before we update go, we will need to remove it from our system. To remove Go from Mac, issue the following command:

To uninstall, delete the /usr/local/go directory by:

$ sudo rm -rf /usr/local/go

Go Language Server

Language server is a LSP protocol capable server that is created to support features like auto complete, go to definition, find all references etc into a IDE. Hence there isn’t a need for various IDE to develop the support independently.

Configuring Visual Studio Code to use Go Language Server

By invoking the command pallette and invoking Preferences:Configuring Language Specific Settings.

Select Go Language
Add the above line

As of 1 February 2021: VS Code Go extension now enables the gopls language server by default.

Changing user name and email in VSCode

Before we can commit changes and push the changes to GitHub, we need to configure the user.name and user.email for VSCode.

Execute the following command via the terminal.

git config --global user.name "goodfunlover"
git config user.name
git config --global user.email "[email protected]"

Range looping in Go

2 ways of looping in go. One is the typical 3 components loop (initialization, test condition, post condition) and the other way is via range.

Syntax for range is as follows:

for k, v := range os.Args[1:] {
  fmt.Println(k)
  fmt.Println(v)
}

The above code reads echo whatever parameter we pass in to the program. k stores the Key and v stores the value from os.Args[]. When we do not need either k or v, we will need to use the blank identifier _ as Golang does not allows unused variables.

for _, v := range os.Args[1:] {
  fmt.Println(v)
}
for k, _ := range os.Args[1:] {
  fmt.Println(k)
}

Shorthand Declaration of Variable Limitation

var desckSize int = 20
deckSize := 20

The above 2 lines of code are the same. := is a shorthand for variable declaration where we leave it to Go to identify the data type of the variable. Thereafter we are not allow to change the data type since Go is a static type programming language.

:= syntax in golang, shorthand declaration of a variable can be only used inside a function body. If other wise we will get :

syntax error: non-declaration statement outside function body
package main

import "fmt"

var deckSize int = 20 // This work
deckSize := 20        // this don't work

Leave a Reply

Back To Top