Skip to content

Commit

Permalink
feat: reto mouredev#11 - go >> [E]
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-empty committed Jun 6, 2024
1 parent c14966a commit 8041dfa
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Retos/Reto #11 - URL PARAMS [Fácil]/go/qwik-zgheib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"strings"
)

type URLParameterExtractor interface {
Extract(url string) ([]string, error)
}

type SimpleURLParameterExtractor struct{}

func NewSimpleURLParameterExtractor() *SimpleURLParameterExtractor {
return &SimpleURLParameterExtractor{}
}

func (e *SimpleURLParameterExtractor) Extract(url string) ([]string, error) {
if !strings.Contains(url, "?") {
return nil, fmt.Errorf("no parameters found in URL")
}

parts := strings.Split(url, "?")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid URL format")
}

paramsPart := parts[1]
params := strings.Split(paramsPart, "&")
values := []string{}

for _, param := range params {
pair := strings.Split(param, "=")
if len(pair) == 2 {
values = append(values, pair[1])
} else {
return nil, fmt.Errorf("invalid parameter format")
}
}

return values, nil
}

func main() {
url := "https://retosdeprogramacion.com?year=2023&challenge=0"
extractor := NewSimpleURLParameterExtractor()

values, err := extractor.Extract(url)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Extracted values:", values)
}

0 comments on commit 8041dfa

Please sign in to comment.