Giter VIP home page Giter VIP logo

golang-notes's Introduction

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

golang-notes's People

Contributors

jobzi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.