SlideShare una empresa de Scribd logo
1 de 43
Descargar para leer sin conexión
GoLang
By: Gagan Chouhan
A Quick Guide
Why Go?
● Go is open-source but backed up by a large corporation
● Many big companies used it for their projects including Google, Dropbox,
Cloudflare, Docker etc.
● Go is fast to (learn, develop, compile, deploy, run)
● Go is a Modern Language
● Go is Simple
● Go is Concurrent
● Go is safe
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
Go file layout
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Hello Go Program
Packages
● Packages that we are going to use in this tutorial
○ fmt :- format
○ log :- logging
○ math :- similar to java.math
○ time :- time related functionality
Imports
● Syntax of import statement :-
import "fmt"
import (
"fmt"
)
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
Types
● bool
● string
● int int8 int16 int32 int64
● uint uint8 uint16 uint32 uint64 uintptr
● byte // alias for uint8
● rune // alias for int32 (in java char)
● float32 float64
● complex64 complex128
● And Pointer of these types ex: *int
● var variable string
variable = "Variable"
● var variable string = "Variable"
● var variable = "Variable"
● variable := "Variable"
Variables, Constants declaration
● Variable declaration
● const variable string = "Variable"
● const variable = "Variable"
Variables, Constants declaration
● Constant declaration
Naming rules: Convention
● Use camelCase
● Acronyms should be all capitals, as in ServeHTTP
● Single letter represents index: i, j, k
● Short but descriptive names: cust not customer
● avoid repeating package name:
○ log.Info() // good
○ log.LogInfo() // bad
Naming rules: Convention
● Don’t name like getters or setters
○ custSvc.cust() // good
○ custSvc.getCust() // bad
● Add er to Interface
○ type Stringer interfaces {
String() string
}
Naming rules: Scope
● Any variable, function etc. name starts with capital letter is considered public
or in golang terms “exported”
○ var variable string = “Variable”
//not exported only visible locally
○ var Variable string = “Variable”
//exported and can be accessed publicly
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
Conditionals
● If (NO BRACKETS)
○ if true {
}
○ if true {
} else if false {
}
Conditionals
● switch (similar to older switch)
○ switch c {
case "one":
case "two":
case "three":
}
//break is not needed here
Conditionals
● switch (instead of if-else)
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
Loops
● for
○ for init; condition; post {
// run commands till condition is true
}
○ for {
// I will run forever
}
Loops
● for
○ for condition {
// run commands till condition is true
// similar to while
}
○ for k,v := range vv {
// I will run for each item
}
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
● With one return value
○ func add(x int, y int) int {
return x + y
}
● With multiple return values
○ func swap (x int, y int) (int, int) {
return y, x
}
Functions: creating
● Single returning
○ z := add (x, y)
● Multi returning
○ a, b := swap(x, y)
● Exported functions
○ fmt.Println(“Hello Go!”)
//fmt is package name
Functions: Using
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
Packages: creating
● Package is a bundle of code
● If there are multiple files that deals with some area, like math deals with
mathematical calculation they should be in same package
● Name and import path should be short descriptive
● No underscores ‘_’ or hyphen ‘-’ example strconv is a
● Folder name should be same as the package name in the code
Packages: Sharing
● How to use a third party package ? “go get” it instead
○ go get github.com/robfig/cron this will import the repo to your
local machine
○ And add the package in import statement
import “github.com/robfig/cron”
● You created a project just push the repository. Any one who is having access
to the repo will be able to import it.
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
● Size is fixed
● As soon as we declare array all values are assigned zero values (default)
○ primes := [6]int{2, 3, 5, 7, 11, 13}
○ var names [2]string
Arrays: defining array
● A slice is a dynamically-sized, flexible view into the elements of an array.
● In practice, slices are much more common than arrays
● We can initialize a slice in following ways.
○ var s []int = primes[1:4] //4 is excluded
○ var s []int = primes[2:]
○ var s []int = primes[:4]
○ a := make([]int, 5)
Slices: defining
● Map is very similar to the map in java
● It is Type safe
● Keys need to be comparable, the equality operators == and != apply to
operands that are comparable.
● We can initialize a map[key]value in following ways.
○ var mymap map[string]int = make(map[string]int)
○ var mymap = make(map[string]int)
○ mymap := make(map[string]int)
Map: defining
● for range loop is there
○ for i, v := range names {
fmt.Printf("%v, %vn", i, v)
}
● If names is a map i -> key, v-> value
Arrays, Slice, Map: iteration
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
● We can define our own data type:-
○ type Minutes int
○ type Hours int
○ type Weight float64
○ type Liters float32
Type definition:user defined types
● A struct is a composite type can have multiple fields
○ type Person struct {
FirstName string
LastName string
Age int
}
○ person1 = Person{}
○ person1 = Person{ FirstName: "Steve ", LastName: "Jobs", Age: 28}
○ person1 = Person{ "Steve ", "Jobs", 28}
Type definition: structs
● Isn’t a method same as function?
No! A method is something that belongs to some object
● How do I define it
○ func (h Hours) ToMinutes() Minutes {
return Minutes(int(h) * 60)
} //here h is called receiver which Before this our defined type
● Similarly you can define methods for structs
Types: methods
● Similarly you can define methods for structs
● Example: calender.Date.go, calender.Event.go
Types: methods
● Similarly you can define methods for structs
● Example: calender.Date.go, calender.Event.go
Types: methods
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
● A defer statement pushes a function call onto a list. The list of saved calls is
executed after the surrounding function returns
● Example: fileio.go
Failure and Recovery:
defer
● Panic is a built-in function that stops the ordinary flow of control and begins
panicking.
● When the function F calls panic,
a. execution of F stops
b. any deferred functions in F are executed normally
c. and then F returns to its caller
● Example: fileio.go
Failure and Recovery:
panic
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
● A goroutine is a lightweight thread managed by the Go runtime
● How to call? Just go followed by function call
a. go say("world")
● Channels are something through which you can send and receive values with
the channel operator
a. c chan int
● Ch <- v (sending value to a channel )
● Receiving value through a channel <-ch
Goroutines and Channels
Things we are going to cover
● Go file layout, packages and imports, IDE
● Types, Variables, Constants, Naming Rules
● Conditionals and Loops
● Functions
● Creating Packages
● Arrays, Slices and map, for...range
● Structs, Defined types, Methods and interfaces
● Failure and recovery
● Goroutines and channels
● How to Write Go Code
● Refer https://golang.org/doc/code.html
● https://go.dev/doc/effective_go
How to write go code

Más contenido relacionado

Similar a Go_ Get iT! .pdf

Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshopadam1davis
 
The GO programming language
The GO programming languageThe GO programming language
The GO programming languageMarco Sabatini
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io
 
Smalltalk, the dynamic language
Smalltalk, the dynamic languageSmalltalk, the dynamic language
Smalltalk, the dynamic languagemohamedsamyali
 
Building parsers in JavaScript
Building parsers in JavaScriptBuilding parsers in JavaScript
Building parsers in JavaScriptKenneth Geisshirt
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
go language- haseeb.pptx
go language- haseeb.pptxgo language- haseeb.pptx
go language- haseeb.pptxArsalanMaqsood1
 
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdfLec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdfRahulKumar342376
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
Web development basics (Part-3)
Web development basics (Part-3)Web development basics (Part-3)
Web development basics (Part-3)Rajat Pratap Singh
 
Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016maiktoepfer
 
Javascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptxJavascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptxMukundSonaiya1
 

Similar a Go_ Get iT! .pdf (20)

Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshop
 
Jade
JadeJade
Jade
 
The GO programming language
The GO programming languageThe GO programming language
The GO programming language
 
Golang
GolangGolang
Golang
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
Ruby meets Go
Ruby meets GoRuby meets Go
Ruby meets Go
 
04-JS.pptx
04-JS.pptx04-JS.pptx
04-JS.pptx
 
04-JS.pptx
04-JS.pptx04-JS.pptx
04-JS.pptx
 
04-JS.pptx
04-JS.pptx04-JS.pptx
04-JS.pptx
 
Smalltalk, the dynamic language
Smalltalk, the dynamic languageSmalltalk, the dynamic language
Smalltalk, the dynamic language
 
Building parsers in JavaScript
Building parsers in JavaScriptBuilding parsers in JavaScript
Building parsers in JavaScript
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
go language- haseeb.pptx
go language- haseeb.pptxgo language- haseeb.pptx
go language- haseeb.pptx
 
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdfLec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Web development basics (Part-3)
Web development basics (Part-3)Web development basics (Part-3)
Web development basics (Part-3)
 
Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016Not Your Fathers C - C Application Development In 2016
Not Your Fathers C - C Application Development In 2016
 
Ready to go
Ready to goReady to go
Ready to go
 
Javascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptxJavascript Programming according to Industry Standards.pptx
Javascript Programming according to Industry Standards.pptx
 

Último

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 

Último (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 

Go_ Get iT! .pdf

  • 2. Why Go? ● Go is open-source but backed up by a large corporation ● Many big companies used it for their projects including Google, Dropbox, Cloudflare, Docker etc. ● Go is fast to (learn, develop, compile, deploy, run) ● Go is a Modern Language ● Go is Simple ● Go is Concurrent ● Go is safe
  • 3. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 4. Go file layout package main import "fmt" func main() { fmt.Println("Hello, Go!") } Hello Go Program
  • 5. Packages ● Packages that we are going to use in this tutorial ○ fmt :- format ○ log :- logging ○ math :- similar to java.math ○ time :- time related functionality
  • 6. Imports ● Syntax of import statement :- import "fmt" import ( "fmt" )
  • 7. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 8. Types ● bool ● string ● int int8 int16 int32 int64 ● uint uint8 uint16 uint32 uint64 uintptr ● byte // alias for uint8 ● rune // alias for int32 (in java char) ● float32 float64 ● complex64 complex128 ● And Pointer of these types ex: *int
  • 9. ● var variable string variable = "Variable" ● var variable string = "Variable" ● var variable = "Variable" ● variable := "Variable" Variables, Constants declaration ● Variable declaration
  • 10. ● const variable string = "Variable" ● const variable = "Variable" Variables, Constants declaration ● Constant declaration
  • 11. Naming rules: Convention ● Use camelCase ● Acronyms should be all capitals, as in ServeHTTP ● Single letter represents index: i, j, k ● Short but descriptive names: cust not customer ● avoid repeating package name: ○ log.Info() // good ○ log.LogInfo() // bad
  • 12. Naming rules: Convention ● Don’t name like getters or setters ○ custSvc.cust() // good ○ custSvc.getCust() // bad ● Add er to Interface ○ type Stringer interfaces { String() string }
  • 13. Naming rules: Scope ● Any variable, function etc. name starts with capital letter is considered public or in golang terms “exported” ○ var variable string = “Variable” //not exported only visible locally ○ var Variable string = “Variable” //exported and can be accessed publicly
  • 14. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 15. Conditionals ● If (NO BRACKETS) ○ if true { } ○ if true { } else if false { }
  • 16. Conditionals ● switch (similar to older switch) ○ switch c { case "one": case "two": case "three": } //break is not needed here
  • 17. Conditionals ● switch (instead of if-else) t := time.Now() switch { case t.Hour() < 12: fmt.Println("It's before noon") default: fmt.Println("It's after noon") }
  • 18. Loops ● for ○ for init; condition; post { // run commands till condition is true } ○ for { // I will run forever }
  • 19. Loops ● for ○ for condition { // run commands till condition is true // similar to while } ○ for k,v := range vv { // I will run for each item }
  • 20. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 21. ● With one return value ○ func add(x int, y int) int { return x + y } ● With multiple return values ○ func swap (x int, y int) (int, int) { return y, x } Functions: creating
  • 22. ● Single returning ○ z := add (x, y) ● Multi returning ○ a, b := swap(x, y) ● Exported functions ○ fmt.Println(“Hello Go!”) //fmt is package name Functions: Using
  • 23. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 24. Packages: creating ● Package is a bundle of code ● If there are multiple files that deals with some area, like math deals with mathematical calculation they should be in same package ● Name and import path should be short descriptive ● No underscores ‘_’ or hyphen ‘-’ example strconv is a ● Folder name should be same as the package name in the code
  • 25. Packages: Sharing ● How to use a third party package ? “go get” it instead ○ go get github.com/robfig/cron this will import the repo to your local machine ○ And add the package in import statement import “github.com/robfig/cron” ● You created a project just push the repository. Any one who is having access to the repo will be able to import it.
  • 26. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 27. ● Size is fixed ● As soon as we declare array all values are assigned zero values (default) ○ primes := [6]int{2, 3, 5, 7, 11, 13} ○ var names [2]string Arrays: defining array
  • 28. ● A slice is a dynamically-sized, flexible view into the elements of an array. ● In practice, slices are much more common than arrays ● We can initialize a slice in following ways. ○ var s []int = primes[1:4] //4 is excluded ○ var s []int = primes[2:] ○ var s []int = primes[:4] ○ a := make([]int, 5) Slices: defining
  • 29. ● Map is very similar to the map in java ● It is Type safe ● Keys need to be comparable, the equality operators == and != apply to operands that are comparable. ● We can initialize a map[key]value in following ways. ○ var mymap map[string]int = make(map[string]int) ○ var mymap = make(map[string]int) ○ mymap := make(map[string]int) Map: defining
  • 30. ● for range loop is there ○ for i, v := range names { fmt.Printf("%v, %vn", i, v) } ● If names is a map i -> key, v-> value Arrays, Slice, Map: iteration
  • 31. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 32. ● We can define our own data type:- ○ type Minutes int ○ type Hours int ○ type Weight float64 ○ type Liters float32 Type definition:user defined types
  • 33. ● A struct is a composite type can have multiple fields ○ type Person struct { FirstName string LastName string Age int } ○ person1 = Person{} ○ person1 = Person{ FirstName: "Steve ", LastName: "Jobs", Age: 28} ○ person1 = Person{ "Steve ", "Jobs", 28} Type definition: structs
  • 34. ● Isn’t a method same as function? No! A method is something that belongs to some object ● How do I define it ○ func (h Hours) ToMinutes() Minutes { return Minutes(int(h) * 60) } //here h is called receiver which Before this our defined type ● Similarly you can define methods for structs Types: methods
  • 35. ● Similarly you can define methods for structs ● Example: calender.Date.go, calender.Event.go Types: methods
  • 36. ● Similarly you can define methods for structs ● Example: calender.Date.go, calender.Event.go Types: methods
  • 37. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 38. ● A defer statement pushes a function call onto a list. The list of saved calls is executed after the surrounding function returns ● Example: fileio.go Failure and Recovery: defer
  • 39. ● Panic is a built-in function that stops the ordinary flow of control and begins panicking. ● When the function F calls panic, a. execution of F stops b. any deferred functions in F are executed normally c. and then F returns to its caller ● Example: fileio.go Failure and Recovery: panic
  • 40. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 41. ● A goroutine is a lightweight thread managed by the Go runtime ● How to call? Just go followed by function call a. go say("world") ● Channels are something through which you can send and receive values with the channel operator a. c chan int ● Ch <- v (sending value to a channel ) ● Receiving value through a channel <-ch Goroutines and Channels
  • 42. Things we are going to cover ● Go file layout, packages and imports, IDE ● Types, Variables, Constants, Naming Rules ● Conditionals and Loops ● Functions ● Creating Packages ● Arrays, Slices and map, for...range ● Structs, Defined types, Methods and interfaces ● Failure and recovery ● Goroutines and channels ● How to Write Go Code
  • 43. ● Refer https://golang.org/doc/code.html ● https://go.dev/doc/effective_go How to write go code