mmcgrana / gobyexample

Go by Example
https://gobyexample.com
7.18k stars 1.26k forks source link

Add embed-directive example #423

Closed peterzhu1992 closed 2 years ago

peterzhu1992 commented 2 years ago

Signed-off-by: peterzhu1992 peterzhu1992@gmail.com

Description

Add embed-directive example and rename embedding to struct-embedding.

Resolved Issue

422

peterzhu1992 commented 2 years ago

@eliben opened a PR, please review, please raise comments if you want me to improve anything. It is my 1st time contribute to this repo so things can be messy. 😄

Thanks.

eliben commented 2 years ago

Sorry, I was meaning to comment this on the issue - let's separate the renaming into its own PR first. Once that one lands, this PR can be rebased and reviewed.

peterzhu1992 commented 2 years ago

Hi @eliben,

Sure, I have put this PR back to WIP. Please approve and merge this renaming 1st.

I will rebase this PR once that one is merge.

Thanks.

peterzhu1992 commented 2 years ago

Hi @eliben,

Thanks for merging #424 and I have also resolved the conflict of this one.

Please review, thanks.

peterzhu1992 commented 2 years ago

@eliben there are some issues with using example_folder.

The reason being that //go:embed requires the files to be presented when compiling. And the existing measure.go/generate.go will panic if sourcePath is a dir when readline.

goroutine 1 [running]: main.check(...) /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/measure.go:14 main.readLines({0xc0001507e0?, 0x0?}) /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/measure.go:20 +0x67 main.main() /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/measure.go:32 +0xa8 exit status 2

panic: read examples/embed-directive/example_folder: is a directory goroutine 1 [running]: main.check(...) /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/generate.go:35 main.mustReadFile(...) /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/generate.go:79 main.readLines({0xc000840bd0?, 0x12f4e7e?}) /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/generate.go:88 +0x67 main.parseSegs({0xc000840bd0?, 0x35?}) /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/generate.go:159 +0x36 main.parseAndRenderSegs({0xc000840bd0, 0x27}) /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/generate.go:234 +0x2d main.parseExamples() /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/generate.go:281 +0x6de main.main() /Users/zhujiaxi/amazonworks/sourcecodes/peterzhuamazon/gobyexample_peterzhu1992/tools/generate.go:348 +0x1d2 exit status 2


It seems like we need to add a check in `measure.go` and `generate.go` to ensure readline does not happen on directories, like:

fileInfo, _ := os.Stat(sourcePath) if !fileInfo.IsDir() {}



Let me know how you think about this, I cannot think of any better solutions now.

PS: I now remember this is the reason I used `embad-directive.hash` as sample file to start with.

Thanks.
eliben commented 2 years ago

Yes, I think it should be fine to skip directories in measure/build.

peterzhu1992 commented 2 years ago

Sure @eliben I am a bit busy these few days, will find a time slot to correct the scripts and push again. Thanks.

peterzhu1992 commented 2 years ago

It is actually easier than I thought, so complete the changes right now. Please review @eliben . Thanks.

peterzhu1992 commented 2 years ago

Hi @eliben I read your follow up tweaks they are truly better. Happy to see it shows up on the main website now.

Thanks.