Package Mainimport "FMT" var x, y Intvar (//This factorization keyword is typically used to declare global variables a int b bool)/* Define struct */type Circle struct {rad Ius float64}type Books struct {title string author string subject string book_id Int}func main () {var a = "Rookie tutorial" C Onst B string = "runoob.com" var c bool = truefmt. Println ("Hello, world!", a,b,c)//classicfor a:=0;a<2;a++ {fmt. Println (a)}//whiled:=0for (d<2) {fmt. Println (d);d ++}//forever loopfor{d++;if (d==10) {fmt. Println ("break"), break}} var c1 circlec1.radius = 10.00fmt. Println ("Area of Circle (C1) =", C1.getarea ()) var Book2 Books/* declares Book2 for Books type */book2.title= "Test.go" book2.a Uthor= "Fsrong" book2.subject= "Trinne" Book2.book_id=110printbooks (&BOOK2)//splitarrsplitarrar ()//rangernage_ T ()//mapmap_t ()//deletedelete_t ()//recursion//factorial (5) fmt. Println (factorial (5))//interfacevar iphone Iphoneiphone.call () var phone phonephone=new (Nokiaphone) Phone.call () Phone=new (IPhone) phone.call ()//error interface//Normal if result, errormsg: = Divide (100, 10); ErrorMsg = = "" {fmt. Println ("100/10 =", result)}//When the divisor is zero returns an error message if _, errormsg: = Divide (100, 0); ErrorMsg! = "" {fmt. Println ("ErrorMsg is:", errormsg)} var div divideerror;div.divider=10;div.dividee=100; Fmt. Println (Div. Error ()) var err error=new (Divideerror) fmt. Println (Err. Error ())}//The method is part of the Circle type Object, func (cc Circle) Getarea () float64 {//c.radius is the property in the Circle type Object return 3.14 * C C.radius * Cc.radius}//poniterfunc Max (a *int,b *int) int {if (*a > *b) {return *a}else{return *b}}//funftionfunc Add ( a int,b int) int {c: = a + bfmt. Println (c) return c}//struct as function parameter func printbooks (Book1 *books) {fmt. Println (Book1.title) fmt. Println (Book1.author) fmt. Println (Book1.subject) fmt. Println (book1.book_id)}//go language Slice (Slice) func Splitarrar () {//split Arrarvar arr =[]int{1,2,3,4}s:=arr[1:]fmt. Println (s) s=arr[2:len (arr)]fmt. Println (s) s1:=make ([]int,3,5) fmt. Println (S1) fmt. Println (Cap (arr))//append 1,2,3s=append (s,1,2,3) fmt. PRINTLN (s)//copy befor Expansion of Consumptions1=make ([]int,len (s), cap (s) * *) copy (s1,s) fmt. PRINTLN (S1) for e: = Range s1{fmt. Println (S1[e])}}//rangefunc rnage_t () {num: = []int{1,2,3,4,5}var sum int=0//index,valuefor _,n: = range Num{sum + = n}fmt. Println (sum) var str string= "ABCD" for I:=range str{fmt. Println (Str[i])}}//mapfunc map_t () {//var strstrmap map[string]string//If the map is not initialized, a nil map is created. Nil map cannot be used to store key value pairs Strstrmap: = Make (map[string]string) strstrmap["A"]= "a" strstrmap["B"]= "B" strstrmap["C"]= "C" strstrmap["D"]= "D" FMT. Println (Strstrmap) for key:= range strstrmap{fmt. Println (Strstrmap[key])}}//deletefunc delete_t () {/* Create map */strstrmap: = map[string]string{"France": "Paris", "Italy" : "Rome", "Japan": "Tokyo", "India": "New Delhi"} fmt. Println ("original map")//* Print map */For Country: = Range Strstrmap {fmt. PRINTLN (Country, "capital is", Strstrmap [country])}/* Delete element */delete (Strstrmap, "France") fmt. PRINTLN ("French entry deleted") Fmt. Println ("delete element after map") for Country: = Range STRSTRMap {fmt. PRINTLN (Country, "capital is", Strstrmap [country])}}//recursion, factorial and Var sum uint64=0func factorial (n uint64) (result UInt64) { if (n > 0) {result = n * factorial (n-1) sum + = result fmt. PRINTLN (SUM) return result} return 1}//interface the method in the interface must be implemented by the user type Phone interface {call ()}//--------- -----------------type nokiaphone struct {}func (nokiaphone nokiaphone) call () {FMT. Println ("I am Nokia, I can call you!")} --------------------------type iphone struct {}func (iphone iphone) call () {FMT. Println ("I am iPhone, I can call you!")} The error type is an interface type, which is its definition of type error interface {error () string}//defines a divideerror struct type divideerror struct {Dividee int divider int}//implements ' Error ' interface func (de *divideerror) error () string {strformat: = ' cannot proceed, the divID ER is zero. Dividee:%d divider:%d ' return FMT. Sprintf (Strformat, De.dividee,de.divider)}//defines the function of the ' int ' type division operation func Divide (Vardividee int, Vardivider int) (rEsult int, errormsg string) {if Vardivider = = 0 {dData: = divideerror{Dividee:vardividee, Divider:vardivider,} errormsg = Ddata.error () return} else {return Vardividee/var Divider, ""}}
Go language Learning