How to check if a feild has "A" "b" or "C"

Hi,

I am trying to create a function that checks if the field has “A” or “B” or “C” plan in it and then the plan payment field should not be empty and if there is nothing in the Plan field then give me an error. Can you plz help me with that?

What have you tried so far?

1 Like

Quoted from withdrawn post above:

func (in *infofile) vplanO() (string, string) { 
    if (in.plan) != (A || A || C ||D) { 
        return "Error", fmt.Sprintf("Invalid plan %s", plan) 
    } 
    return "", "" 
}

what are all these A || A || C || D?
where does plan variable comes from at the end of return statement?
why is this infofile passed as a pointer?
what IS infofile?

If plan is a string, you can easily use strings package (funcs like Contains, Count, etc)

A B C d are the plans
variable comes from the infofile struct
because infofile has a struct

this explains nothing at all, what sort of data is it?

compiler won’t be able to just guess that, unless it is a global variable (which there shouldn’t be any really), did you mean in.plan and not just plan?

what is infofile then?

Data is string

infoline is a struct name, and plan is a field name

is it infoline or infofile, pick one :slight_smile:

Anyway, is this something you are looking for (not literally of course, but the idea itself)?

package main

import (
	"errors"
	"fmt"
)

type infofile struct {
	plan string
}

func (in infofile) vplanO() error {
	for _, e := range []string{"A", "B", "C", "D"} {
		if in.plan == e {
			return nil
		}
	}
	return errors.New("plan doesn't exist")
}

func main() {

	infos := []infofile{
		infofile{"A"},
		infofile{"Z"},
	}

	for _, e := range infos {
		err := e.vplanO()
		if err != nil {
			fmt.Printf("Infofile: %+v, err: %s\n", e, err)
			continue
		}
		fmt.Printf("Infofile: %+v is good\n", e)
	}
}

or do you want to check whether the plan ends (or begins) with one of those letters?

Maybe like this?

I think it is easier just do

func (in infofile) vplanO() error {
    if  strings.Contains("ABCD", in.plan) {
      return nil
    }
	return errors.New("plan doesn't exist")
}
1 Like

That is if the plan names are in fact one letter strings, which is quite doubtful, yet possible.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.