Skip to main content

Posts

Generics in Rust

Generics in Rust Example 1: #[derive(Debug)] struct Point<T, U, V> { x: T, y: U, z: V, } fn main() { let point1 = Point{ x: 1, y: 1, z: 1.0, }; let point2 = Point{ x: 1.0, y: 1.0, z: 1, }; let point3 = Point { x: 'x', y: 'y', z: 1, }; println!("{:?}", point1); println!("{:?}", point2); println!("{:?}", point3); } Example 2: #[derive(Debug)] struct Point<T> { x: T, y: T, } impl<T> Point<T> { fn x(&self) ->&T { &self.x } } fn main() { let p = Point {x: 5, y: 10}; println!("{:?}", p.x()); } Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

English Words: Monday, 18 February 2019

English Words: Monday, 18 February 2019 1. Abort Bring to a premature end because of a problem or fault 2. Cobweb A spider's web 3. Feeding An act of giving food 4. Immunity The ability of an organism to resist a particular infection 5. Incredible Impossible to believe 6. Insane In a state of mind which prevents normal perception, behaviour 7. Insist Demand something forcefully 8. Pimp A man who controls prostitutes and arranges clients for them 9. Retention The continued possession 10. Thrilled Cause (someone) to have a sudden feeling of excitement and pleasure Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

English Words: Sunday, 17 February 2019

English Words: Sunday, 17 February 2019 Bitch A female dog Difficult or unpleasant situations make spitefully critical comments Compulsive Relating to an irresistible urge Crib Copy (another person's work) illicitly or without acknowledgement Curious Eager to know or learn something Grumpy Bad-tempered and irritable Hostile Showing or feeling opposition or dislike Impair weaken or damage Liberal Willing to respect or accept behaviour or opinions different from one's own Therapist A person who treats psychological problems; Prosperity  The state of being prosperous. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Rust Variables

Rust Variables In this post, we will learn some important fact about variable of a rust programming language. Point to Remember 1. Rust variables can be type annotated let logical: bool = true; 2. Default Rust variable is immutable so we can not change its value let logical: bool = true; // Error logical = false; 3. Mutable variable value can be changed let mut logical: bool = true; // valid logical = false; 4. Type of Rust variable can not be changed let mut mutable = 5; // Error mutable = true; 5. Rust variables can be overwritten with shadowing let mut mutable = 5; // valid let mutable = true; Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GO API: Utill

GO API: Utill constant.go package utils const PORT string = "9090" const COCKROACHDB_USERNAME_ENV string = "username" const COCKROACHDB_DB_ENV string = "students" const COCKROACHDB_HOST_ENV string = "localhost" const COCKROACHDB_PORT_ENV string = "26257" rest_util.go package utils import ( "encoding/json" "net/http" ) func WriteHttpResponse(w http.ResponseWriter, v interface{}, err error) { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } js, err := json.Marshal(v) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) w.Write(js) } func WriteEmptyHttpResponse(w htt...

GO API: Model

GO API: Model goapi/server/src/test.com/goapi/app/model/student.go package model type Student struct { // Roll Number of the student RollNumber int `json:"roll_number,omitempty"` // Name of the student Name string `json:"name,omitempty"` // Branch of the student Branch string `json:"branch,omitempty"` // Year of admission Batch string `json:"batch,omitempty"` } Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GO API: API Definition

GO API: API Definition student.go package apis import ( "github.com/gorilla/mux" . "test.com/goapi/app/utils" . "test.com/goapi/app/db" . "test.com/goapi/app/model" "net/http" "encoding/json" "strconv" ) func GetAllStudentInfo(w http.ResponseWriter, r *http.Request) { students := GetStudentsDB() WriteHttpResponse(w, students, nil) } func GetStudentInfo(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) rollNumber := params["student-id"] student := GetStudentDB(rollNumber) WriteHttpResponse(w, student, nil) } func CreateStudentInfo(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() decoder := json.NewDecoder(r.Body) var student Student err := decoder.Decode(&student) if err == nil { ...

GO API: Database Connection

GO API: Database Connection client_db.go package db import ( "database/sql" _ "github.com/lib/pq" . "test.com/goapi/app/utils" "sync" "log" ) var ( db *sql.DB ConnOnce sync.Once ) // create database connection func GetDBConnection() *sql.DB { var err error ConnOnce.Do(func() { db, err = sql.Open("postgres", "postgresql://"+COCKROACHDB_USERNAME_ENV+"@"+COCKROACHDB_HOST_ENV+":"+COCKROACHDB_PORT_ENV+ "/"+COCKROACHDB_DB_ENV+"?"+"sslmode=disable") }) if err != nil { log.Fatal("error connecting to the database: ", err) } return db } func ExecuteQuery(query string, args string) *sql.Rows{ db := GetDBConnection() if args == "" { rows, ...

GO API: MAIN FUNC

GO API: MAIN FUNC package main import ( sw "test.com/goapi/app" "log" "net/http" ) func main() { router := sw.NewRouter() log.Println("listen on:9090") log.Fatal(http.ListenAndServe(":"+"9090", router)) } Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GO API: Router

GO API: Router In this post, we will define router for our GOLANG API project. For now, copy below code and paste it in router.go file package app import ( "github.com/gorilla/mux" . "test.com/goapi/app/apis" "net/http" "fmt" ) type Route struct { Name string Method string Pattern string HandlerFunc http.HandlerFunc } type Routes []Route func NewRouter() *mux.Router { router := mux.NewRouter().StrictSlash(true) for _, route := range routes { var handler http.Handler handler = route.HandlerFunc router. Methods(route.Method). Path(route.Pattern). Name(route.Name). Handler(handler) } return router } func Index(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "...