Solution Please... cannot use type string as type *string


(Hardeen L) #1
			package main

			import (
				"html/template"
				"os"
				"fmt"
				_ "github.com/go-sql-driver/mysql"
				"database/sql"
				"strconv"
			)




			var tmplSrc = `
			{{range $i, $a := .Title}}
			  Title: {{$a}}-{{.DD}}-{{.FF}}
			  {{range $article := index $.Article $i}}
				Article: {{$article}}.
			  {{end}}
			{{end}}`


			func dbConn() (db *sql.DB) {
				dbDriver := "mysql"
				dbUser := "root"
				dbPass := ""
				dbName := "ask"
				db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
				if err != nil {
					panic(err.Error())
				}
				return db
			}

			type a struct {
				Title   []b
				Title1  []c
				Article [][]string
			}
			type b struct{
				DD   string
				FF int
			}
			type c struct{
				CC string
				EE string
			}

			type d struct{
				DD string
				
			}

			func main() {
				xx:=b{}
				Title:=[]b{}
				
				yy:=c{}
				Title1:=[]c{}

				Article:=[][]string{}
				
				for i:=0; i<=2; i++{
					xx.DD=strconv.Itoa(i)+"a"
					xx.FF=i
					Title=append(Title,xx)
					
					Title1=nil
					for ii:=0; ii<=2; ii++{
						yy.CC=strconv.Itoa(ii)+"b"
						yy.EE=strconv.Itoa(ii)+"c"
						Title1=append(Title1,yy)
					}
					fmt.Println(Title1)
					Article=append(Article,Title1)	
				}


				var data = &a{
					Title: Title,
					Article: Article,
				}
				fmt.Println(data)
				tmpl := template.Must(template.New("test").Parse(tmplSrc))
				tmpl.Execute(os.Stdout, data)
			}

Thanks


(Norbert Melzer) #2

Question please…

I’m not sure what your post is about…

Your code as it is will not work on play.golang.org as it requires external dependencies.

Please try to reduce it to a minimal example, very often one is able to find the solution to his problem during this process on his own.

After reducing the problem to the essential parts, you can add this minified code here or give a link to a playground.


(Hardeen L) #3

im getting this error

cannot use Title1 (type []c) as type []string in append
on this line.

Article=append(Article,Title1)	

Please help


(Jakob Borg) #4

To a slice of strings you can either append a string, or use the dot-dot-dot syntax to append all the elements of another slice of strings. Your example is in between those two working variants, so you need to decide if Title1 should be a string or if you mean Title1....

Here’s a good article about slices: https://blog.golang.org/go-slices-usage-and-internals


(Hardeen L) #5

Thank you sir for your reply. but i think Title1… is not the solution. Bellow is the output im trying to figure out.

Title: {0a 0}-0a-0

Article: {0b 0c} .

Article: {1b 1c}

Article: {2b 2c}

Title: {1a 1}-1a-1

Article: {0b 0c} .

Article: {1b 1c}

Article: {2b 2c}

Title: {2a 2}-2a-2

Article: {0b 0c} .

Article: {1b 1c}

Article: {2b 2c}


(Norbert Melzer) #6

Which you did not even mention in your original post, instead you have choosen a title which suggests that you are mismatching a string and a pointer to a string somewhere…

Please try to ask your questions the smart way.

Do not just throw code at the forum and assume that everyone will be knowing what you are working at. Especially do not assume, that everyone is willing to download libraries which are potentially unrelated to your problem (the mysql driver in this case).

Also please do actually provide a question and not just code and “thanks”…


(Hardeen L) #7

sorry for that sir.


(Norbert Melzer) #8

Well, its actually you hurting yourself.

Currently @calmh, @lutzhorn and me are in a mood to pull the details out of your noses, but currently, there are a lot of threads, which are just a near-to-useless topic and a bulk of code which is partially unrelated to the problem and not runnable at all because of missing pieces.

Our mood to ask for the details might vanish if it continous to feel as if there is no effort by you and the other ones.

Also we have often enough given away tipps about how to ask a question, but at least I do not see that the quality of question is getting better.

So at the end, you need to say sorry to yourself…


(Hardeen L) #9

next time i make it more clear…


(Hardeen L) #10

Thanks @calmh…I got it from your advice. Intelligent people giving a small piece of advice but very useful. I did it this way.

	type a struct {
		Title   []b
		Title1  []b
		Article [][]b
	}
	type b struct{
		DD   string
		FF int
	}
	type c struct{
		CC string
		EE string
	}

	type d struct{
		DD string
		
	}

	func main() {
		xx:=b{}
		Title:=[]b{}
		
		yy:=b{}
		Title1:=[]b{}

		Article:=[][]b{}
		
		for i:=0; i<=2; i++{
			xx.DD=strconv.Itoa(i)+"a"
			xx.FF=i
			Title=append(Title,xx)
			
			Title1=nil
			for ii:=0; ii<=2; ii++{
				yy.DD=strconv.Itoa(ii)
				Title1=append(Title1,yy)
			}

			fmt.Println(Title1)
			Article=append(Article,Title1)
		}
			

		var data = &a{
			Title: Title,
			Article: Article,
		}
		fmt.Println(data)
		tmpl := template.Must(template.New("test").Parse(tmplSrc))
		tmpl.Execute(os.Stdout, data)
	}

result… this is i’m looking for. Thanks

Title: {0a 0}-0a-0

-Article: {0 0}.

-Article: {1 0}.

-Article: {2 0}.

Title: {1a 1}-1a-1

-Article: {0 0}.

-Article: {1 0}.

-Article: {2 0}.

Title: {2a 2}-2a-2

-Article: {0 0}.

-Article: {1 0}.

-Article: {2 0}.


(system) #11

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