Skip to content

Jobzi/golang-notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GOLANG NOTES

This is a Golang Notes

Hello Word for learn golang. I used diferents resources like w3school, golang references api.

  • Structure Basic
    package main  

    import ("fmt")

    //@main this a main fuction
    func main() {  
        fmt.Println("Hello World!")
    }

Go Data Types

Data type is an important concept in programming. Data type specifies the size and type of variable values.

Go is statically typed, meaning that once a variable type is defined, it can only store data of that type.

Go has three basic data types:

  • bool: represents a boolean value and is either true or false
  • Numeric: represents integer types, floating point values, and complex types
  • string: represents a string value
package main
import ("fmt")

func main() {
  var a bool = true     // Boolean
  var b int = 5         // Integer
  var c float32 = 3.14  // Floating point number
  var d string = "Hi!"  // String

  fmt.Println("Boolean: ", a)
  fmt.Println("Integer: ", b)
  fmt.Println("Float:   ", c)
  fmt.Println("String:  ", d)
}

VARIABLES RULES

  • A variable name must start with a letter or an underscore character (_)
  • A variable name cannot start with a digit
  • A variable name can only contain alpha-numeric characters and - underscores (a-z, A-Z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three - different variables)
  • There is no limit on the length of the variable name
  • A variable name cannot contain spaces
  • The variable name cannot be any Go keywords

Variable Declaration With Initial Value

If the value of a variable is known from the start, you can declare the variable and assign a value to it on one line:

package main
import ("fmt")

func main() {
  var student1 string = "John" //type is string
  var student2 = "Jane" //type is inferred
  x := 2 //type is inferred

  fmt.Println(student1)
  fmt.Println(student2)
  fmt.Println(x)
}

Variable Declaration Without Initial Value

In Go, all variables are initialized. So, if you declare a variable without an initial value, its value will be set to the default value of its type

package main
import ("fmt")

func main() {
  var a string
  var b int
  var c bool

  fmt.Println(a)
  fmt.Println(b)
  fmt.Println(c)
}

Go Multiple Variable Declaration

In Go, it is possible to declare multiple variables in the same line.

package main
import ("fmt")

func main() {
  var a, b, c, d int = 1, 3, 5, 7

  fmt.Println(a)
  fmt.Println(b)
  fmt.Println(c)
  fmt.Println(d)
}

Constant Rules

  • Constant names follow the same naming rules as variables
  • Constant names are usually written in uppercase letters (for easy - identification and differentiation from variables)
  • Constants can be declared both inside and outside of a function

Multiple Constants Declaration

Multiple constants can be grouped together into a block for readability:

package main
import ("fmt")

const (
  A int = 1
  B = 3.14
  C = "Hi!"
)

func main() {
  fmt.Println(A)
  fmt.Println(B)
  fmt.Println(C)
}

CHECK DOCUMENTATION ABOUT

About

This is a notes about Goolang

Topics

Resources

Stars

Watchers

Forks

Languages