Minggu, 09 Oktober 2016

Learning Golang (Part 2)

Now your first hello world Golang programs should already worked, let's see what is the meaning of each line.


package main

import "fmt"

func main() {
   /* This is my first sample program. */
   fmt.Println("Hello, World!")
}
  1. The first line of the program package main defines the package name in which this program should lie. It is a must statement as Go programs runs in packages. main package is the starting point to run the program. Each package has a path and name associated with it.
  2. The next lineimport "fmt" is a preprocessor command which tell the Go compiler to include files lying in package fmt.
  3. The next line func main() is the main function where program execution begins.
  4. The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program. Comments are also represented using // similar to Java or C++ comments.
  5. The next line fmt.Println(...) is another function available in Go which causes the message "Hello, World!" to be displayed on the screen. Here fmt package has exported Println method which is used to display message on the screen.
  6. Notice the capital P of Println method. In Go language, a name is exported if it starts with capital letter. Exported means that a function or variable/constant is accessible to importer of the respective package.

Execute Golang Program

   In part 1, we execute Golang program using a command prompt. In this occassion I'll share how to use a plugin that will help you execute it in Notepad++.
After you install Golang installable archieve :
  1. Open Notepad++
  2. Select Plugins > Plugin Manager > Show Plugin Manager (Image 1)
  3. Search for GOnpp
  4. Install
  5. Enjoy

GOnpp assists you writing Go-programms. It has code completion and function calltips (using gocode, see below) as well as direct interaction with the go command. Currently the following actions are implemented:
gocode complete -> ALT+O || gocode calltip -> ALT+P
go fmt -> ALT+F || go test -> ALT+T || go install -> ALT+I || go run -> ALT+R
Note : If you install Golang in a custom location, you need to add GOROOT and GOBIN in system's environment variables


Highlight Golang Codes

This is for Notepad++ too, I have a custom highlight xml for golang. You can download it here. Now, to apply it :
  1. Open Notepad++
  2. Select Language > Define your language
  3. Select import > Navigate to downloaded golang.xml
  4. Save
  5. You may need to restart Notepad++
  6. Select Language > Go
  7. Enjoy


Line Seperator

   Actually I think Golang is pretty similar to C++, it has pointer, struct, no inheritance, but there're also some differences. One of them is the line separator. In Go program, the line seperator key is a statement terminator. That is, each individual statement don't need a special seperator like ; in C. Go compiler internally places ; as statement terminator to indicate the end of one logical entity.


fmt.Println("Hello, World!")
fmt.Println("I am in Go Programming World!")
So, basically the codes above will not produce an error. It will works normally.
It is good, we don't need to waste time when there's a dumb error because of ;.
But, on the other side codes below will produces error. 
if(goal = 10)
{
    fmt.Println("Finished")
}
You have to change it to be like this 
if(goal = 10){
    fmt.Println("Finished")
}
Why? Because end of each line is matters.Well it depends on your style, but for me this is annoying hahaha...


Identifiers

A Golang identifier is a name used to identify a variable, function, or any other user-defined item. Basically, you can use any character except punctuation characters such as  @, $, and % within the identifiers. These are some acceptable identifiers :
mouse       i

_animal     source_File

Kingdom     myAddress

For details, you can access my source link below (tutorialspoint, it is really good). 

Variables

There are 2 ways to create a variables in Golang :

  • Define the data type by yourself
var list_variable_names optional_data_type /*This is the format to create variable*/
var   c, ch int
var  f, salary float32
c = 42;
  • Let Golang decide the data type (only for primitive data types)
package main

import "fmt"

func main() {
   var x float64 = 20.0

   y := 42 
   fmt.Println(x)
   fmt.Println(y)
   fmt.Printf("x is of type %T\n", x)
   fmt.Printf("y is of type %T\n", y) 
}

And, the result is
20
42
x is of type float64
y is of type int

You can also declare a multiple type variables in 1 line

var number1, number2, text = 10, 20, "multiple type variables"

Source :
- https://www.tutorialspoint.com/go/go_program_structure.htm






Tidak ada komentar:

Posting Komentar