Golang Grep

679 views
Skip to first unread message

ToSuNuS

unread,
Jun 15, 2016, 11:54:18 AM6/15/16
to golang-nuts
Hi guys,

How do I assign a variable function of sample scripts are used on the following address?

https://github.com/StefanSchroeder/Golang-Regex-Tutorial/blob/master/01-chapter3.markdown

How can I define this as a variables of function .

grep (flag.arg (0) flag.arg (1))

I tried as follows. However, the results did not.

f :=
grep (flag.arg (0) flag.arg (1))

Regards.

Steven Blenkinsop

unread,
Jun 15, 2016, 11:58:16 AM6/15/16
to ToSuNuS, golang-nuts
Be careful with case. It should be flag.Arg(0), not flag.arg(0). Also, you're missing the comma between arguments. It should be:

f := grep(flag.Arg(0), flag.Arg(1))

Note the comma between the two arguments.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ToSuNuS

unread,
Jun 15, 2016, 12:17:29 PM6/15/16
to golang-nuts
Hi Steven,

I mistyped the opening question.

However, the script has been added correctly.

Of course,
the result is still the same.


I run the command.

go run test.go testword /root/testfile.txt

Result:

grep(flag.Arg(0), flag.Arg(1)) used as value

I just add the following code. (to test)

        f := grep(flag.Arg(0), flag.Arg(1))
        fmt.Println(f)


Regards.

ToSuNuS

unread,
Jun 15, 2016, 12:22:42 PM6/15/16
to golang-nuts
Hi,

I forgot to mention.

I'm new to this.


Regards.

On Wednesday, June 15, 2016 at 6:54:18 PM UTC+3, ToSuNuS wrote:

Steven Blenkinsop

unread,
Jun 15, 2016, 12:28:53 PM6/15/16
to ToSuNuS, golang-nuts
Oh, sorry, didn't see that. `grep` doesn't return a value, so there's nothing to assign to a variable. What are you trying to do exactly? If you want a function value you can call repeatedly which will pass the same arguments to `grep`, you want:

   f := func() { grep(flag.Arg(1), flag.Arg(2)) }

This is called a closure. It basically creates a function value you can call, and it will execute the body each time, in this case the call to grep. It will also capture any variables referenced inside it, but that doesn't happen here.

--

ToSuNuS

unread,
Jun 15, 2016, 1:13:28 PM6/15/16
to golang-nuts, osman...@gmail.com
Hi Steven,

I want to use a word that the data from this function.

for example;

f := func() { grep(flag.Arg(0), flag.Arg(1)) }

if f  == "domain.com" {
}

or

handle := exec.Command("echo", string(f[1])).Output()

Examples of these subject. My purpose is to convert the string.

Regards.

Steven Blenkinsop

unread,
Jun 23, 2016, 5:06:20 PM6/23/16
to ToSuNuS, golang-nuts
Sorry for the late reply, I've been having a busy week. Hopefully you've figured it out by now. If not, I'll point out that the "grep" function doesn't return its results, it prints them out. I've restructured it to make it more general purpose, so that you can use the results in other code:


Basically, now rather than printing matches, it creates a slice containing all the matches and returns that to the caller. It also takes an io.Reader instead of a filename now, since that's more general. If you still want to use a filename, you can open the file outside the grep function and pass it in as the source. Let me know if you have any questions about any part of the code. It's good to develop an understanding of what the code is doing and why, rather than just copying off the internet.
Reply all
Reply to author
Forward
0 new messages