Skip to main content

Posts

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, "...

GO API: Database

GO API: Database In this post, we will create database for our GOLANG API project. For this series for tutorial we are using cockroachDB CockroachDb If you have not installed cockroachDb yet. Please follow this post  Install CockroachDB . Create Database Start a CockroachDB node and access cockroachDB client shell. If you are not familiar with this you can follow this post  CockroachDB Client Command . Once you have access to cockroachdb client create database  Create a User Create database Create table Grant All access to User Create User Create cockroachDB user  CREATE USER USERNAME; Create Database CREATE DATABASE students; Create Table use students; CREATE TABLE students.student_tlb ( roll_number INT PRIMARY KEY, name STRING, batch STRING, branch STRING ); Grant Access GRANT ALL ON TABLE students.* TO USERNAME; Extra Let's insert some dummy data into student_tlb...

Sequence and Series

Sequence and Series We have heard about sequence and Series. But before diving deep into Sequence and Series, let us understand what is a sequence and what is a series? There are cases where even a small discrepancy in measurements can lead to a big trouble. In all other cases, measuring something is made easier by approximations. And as mathematicians, we like to do that a lot! To measure a quantity x, let us write several approximations a1 , a2 , a3… where a2 is a better approximation of x than a1,a3 is better approximation of x than a2 and so on. This list of approximations that we just created is example of a sequence. Definition 1: A sequence is the set of the outputs of a function defined from the set of natural numbers to the set of real numbers or complex numbers. If the co-domain of the function is the set of real numbers, it is called a real sequence and if it is the set of complex numbers on the other hand, it is called a complex sequence. In very simple terms, a ...

Mixture and Alligation

Mixture and Alligation Mixture and alligation is an integral topic in the Quantitative section of the CAT exam. There are a lot of questions asked every year from this topic and thus, is very crucial from the examination point of view. So, here is an explanation of the Mixture and Alligation along with various illustrations to help the candidates learn the topic with ease. Definitions of Mixture and Alligation Here are some important definitions related to mixtures and alligation that would help to tackle the related questions with ease. Mixtures When two or more components are mixed in a certain ratio, a mixture is created. Types of mixtures Simple Mixtures: When two or more different ingredients are mixed together, a simple mixture is formed. Compound Mixtures: When two or more simple mixtures are mixed together, a compound mixture is formed. Alligation To understand the concept of alligations, let’s take an example and look at it from all angles possible. Examp...