This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$numbers = [7, 4]; | |
foreach($numbers as $number) | |
{ | |
if (isPrime($number)) | |
{ | |
echo “$number is a prime number!\n”; | |
}else { | |
echo “$number is a not prime number…\n”; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isPrime($numberToCheck) { | |
for($n = $numberToCheck- 1; $n > 1; $n--) { | |
if (!($numberToCheck% $n)) { | |
return false; | |
} | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isPrime($numberToCheck) { | |
for($i=0, $n=$numberToCheck-1; $n>1; $n--, $i++) { | |
yield $i; | |
if (!($numberToCheck % $n)) { | |
return false; | |
} | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func Talk() { | |
fmt.Println("Hello FROM PLUGIN!!!") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var chain []middleware //empty chain | |
func main() { | |
chain = append(chain, pass("GET|POST")) | |
http.HandleFunc("/", Chain(controller, chain...)) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func pass(args string) func(http.HandlerFunc) http.HandlerFunc { | |
return func(f http.HandlerFunc) http.HandlerFunc { | |
// Define the http.HandlerFunc | |
return func(w http.ResponseWriter, r *http.Request) { | |
//split args and check if the request as this method | |
acceptedMethods := strings.Split(args, "|") | |
for _, v := range acceptedMethods { | |
if r.Method == v { | |
// Call the next middleware in chain | |
f(w, r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Chain(f http.HandlerFunc, mids ...middleware) http.HandlerFunc { | |
for _, m := range mids { | |
f = m(f) | |
} | |
return f | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// middleware filter incoming HTTP requests. | |
// if the request pass the filter, it calls the next HTTP handler. | |
type middleware func(http.HandlerFunc) http.HandlerFunc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
) | |
func controller(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hi there!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"fmt" | |
"plugin" | |
) | |
func main() { | |
//open plugin file | |
plug, err := plugin.Open("plugins/first.so") | |
if err != nil { |
OlderNewer