go run hello.go
..........................................................................................................................................................................................................
Example :#1 [Hello world Example ]
package main
import "fmt"
func main(){
fmt.Println("Hello World!")
}
..........................................................................................................................................................................................................
Example :#2 [ How to create a variable? ]
package main
import "fmt"
func main(){
var name ="Hello GoLang!"
fmt.Println(name)
}
..........................................................................................................................................................................................................
Example :#3 [ Special Character ]
- we can't use any special character like @ or similar
- only use underscore or _
package main
import "fmt"
func main(){
var @name ="Hello GoLang....."
fmt.Println(@name)
}
Error
..........................................................................................................................................................................................................
Example :#4 [ ./prog.go:6:5: assignment mismatch: 1 variables but 2 values ]
package main
import "fmt"
func main(){
var name = "Hello", "World"
//./prog.go:6:5: assignment mismatch: 1 variables but 2 values
fmt.Println(name)
}
..........
Solutions , How to do that
package main
import "fmt"
func main(){
var name, welcome = "Hello", "World"
fmt.Println(name, welcome)
}
variable
package main
import "fmt"
func main(){
var name, welcome = "Hello", "World"
var place
place ="New Delhi"
fmt.Println(name, welcome, place)
}
Error ./prog.go:7:10: syntax error: unexpected newline, expecting type
package main
import "fmt"
func main(){
var name, welcome = "Hello", "World"
var place string
place ="New Delhi"
fmt.Println(name, welcome, place)
}
.................................
package main
import "fmt"
func main(){
var name, welcome = "Hello", "World"
var place string
place =123 // does not work
fmt.Println(name, welcome, place)
}
[Short Variable ]
package main
import "fmt"
func main(){
welcome:="Hello World.."
fmt.Printf("%T", welcome)
}
//String
package main
import "fmt"
func main(){
welcome:=23455
fmt.Printf("%T",welcome)
}
//int
package main
import "fmt"
func main(){
welcome:=2*2
fmt.Print(welcome)
}
//4
Example :#5 [ Rest API ]
package main
import (
"fmt"
"net/http"
"log"
)
func homePage(w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "Hello golang developer!!!!")
}
func RequestPage(){
http.HandleFunc("/",homePage )
log.Fatal(http.ListenAndServe(":8081", nil))
}
func main(){
RequestPage()
}
..........................................................................................................................................................................................................
Example :#6 [ ]
..........................................................................................................................................................................................................
Example :#7 [ ]
..........................................................................................................................................................................................................
Example :#8 [ ]
..........................................................................................................................................................................................................
Example :#9 [ ]
..........................................................................................................................................................................................................
Example :#10 [ ]
..........................................................................................................................................................................................................
Example :#11 [ ]
..........................................................................................................................................................................................................
Example :#12 [ ]
..........................................................................................................................................................................................................
Example :#13 [ ]
..........................................................................................................................................................................................................
Example :#14 [ ]
..........................................................................................................................................................................................................
Example :#15 [ ]
No comments:
Post a Comment